help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Clarification needing regarding auto-generating prerequisites


From: John Graham-Cumming
Subject: Re: Clarification needing regarding auto-generating prerequisites
Date: Wed, 18 May 2005 13:49:58 -0400

On Tue, 2005-05-17 at 10:30 -0700, Thulasidhar JK wrote:
> Hello,
> I am a novice to complicated makefiles and today while trying to write
> a makefile that generates the included C headers automatically from C
> source, i came across this piece of code in the Make manual
> (http://www.gnu.org/software/make/manual).
> 
> %.d: %.c
>         @set -e; rm -f $@; \
>          $(CC) -M $(CPPFLAGS) $< > address@hidden; \
>          sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < address@hidden > $@; \
>          rm -f address@hidden
> 
> i understand that the CC with -M outputs the included headers and the
> source in a makefile target: rule fashion. but the use of sed is what
> is getting me into confusion.
> As the example quotes,
>         main.o : main.c defs.h
> gets generated when the `$(CC) -M $(CPPFLAGS) $<` is executed
> but what is the use of converting this output into 
>         main.o main.d: main.c defs.h
> using sed?
> why should the .d file depend on the include file of the source, when
> it already has a rule that says .d depends upon .c . Even if the
> defs.h changes, how would that affect the contents of main.d i.e., why
> should main.d be updated if defs.h changes?

Somewhere in the Makefile your are going to 'include' those .d files.
When GNU Make sees an included Makefile it will try to remake it
automatically (that's what the %.d : %.c rule is for).   The
relationship between main.d and main.c and defs.h is added so that if
either the .c or .h file changes then the .d is updated.  This is done
because an edit to either of those files could be the addition of a
new .h file that needs to be added to the dependency list.

There's a clear explanation of all this in Managing Project with GNU
Make (O'Reilly), or I grab the presentation "Managing Dependencies" from
www.jgc.org.

John.
-- 
John Graham-Cumming

Home: http://www.jgc.org/
Work: http://www.electric-cloud.com/
POPFile: http://getpopfile.org/
GNU Make Standard Library: http://gmsl.sf.net/






reply via email to

[Prev in Thread] Current Thread [Next in Thread]