l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
April 21: Google Glass
Next Installfest:
TBD
Latest News:
Mar. 18: Google Glass at LUGOD's April meeting
Page last updated:
2003 Mar 22 08:22

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] makefile question - auto dependencies
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] makefile question - auto dependencies



On Fri, Mar 21, 2003 at 01:23:48PM -0800, Peter Jay Salzman wrote:
> begin Mike Simons <msimons@moria.simons-clan.com> 
> >   I used to use a nasty sed expression to make the .d files, but for
> > some reason decided that they were not needed.
>  
> yes again.  :)   and user supplied .h files as well.
>
> >   Here is a current sample of Makefile which I use to build programs
> > ################
> > %.o: %.c
> > 	gcc ${CFLAGS} -c $^
> > 
> > .%.d: %.c
> > 	@gcc -MM ${CFLAGS} -o $@ $^
>
> if i'm not mistaken, you're ignoring the dependencies of the .d files on
> the .c and .h files.  in other words, your .d file will look like:

  The .d on .c is handled by the second rule above.  The .d on .h's are
not handled.  It looks like I grabbed a too simple version of the
makefile from my working directories... one that doesn't handle .h's
correctly at all.

> when it should look like:
>    main.o main.d : main.c main.h
> 
> that's what the awful sed script does (i think).

  Yes, that is what the sed does... you are correct that the .d file
wouldn't reference itself in the .d, so it wouldn't be rebuilt.
  I think that sed was one I supplied... there are at least three 
generations of that expression, the current one looks like:

====
.%.d: %.c
	@$(SHELL) -ec '$(CC) -M $(CFLAGS) $< \
  | sed '\''s/\($*\)\.o[ :]*/\1.o $@ : /g'\'' > $@; \
#	@touch $@    # this touch makes the Ultrix know the file has changed
  [ -s $@ ] || rm -f $@'

====

  The rm will clean up if the gcc line builds a empty depend file...
if for some reason the file is badly broken, but it doesn't abort the
build at that point... I don't remember why I used the SHELL syntax...
The touch was for some borked Ultrix machines in the UCD CS labs, which
used NFS and for some reason didn't notice the files had changed.

  I don't see why this would not work too:
===
.%.d: %.c
	@$(CC) -MM $(CFLAGS) $< | sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' > $@
#	@touch $@    # this touch makes the Ultrix know the file has changed
	@[ -s $@ ] || rm -f $@
	@[ -f $@ ]

===

  The last -f rule will cause Make to abort if the .d file isn't built.
It's an error after all.  :)

  The -MM should change to -M if your '#include <a>' entries might
change (like in a very complex source package).


> also, is the .o->.c explicit rule really necessary?  make already knows
> how to make .o files from .c files.

  It isn't really needed... but I never know for sure what the implicit
one will do so I put one in.

  Anyway I don't have time to dig through the different Makefiles and
put up one that is most good right now.  If you want to check back after
we get a projector and the lugod spring activities are worked out I
should have time then.

    TTFN,
      Mike
_______________________________________________
vox-tech mailing list
vox-tech@lists.lugod.org
http://lists.lugod.org/mailman/listinfo/vox-tech



LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
EDGE Tech Corp.
For donating some give-aways for our meetings.