Mail Archives: djgpp/1997/03/02/16:57:02
Eli Zaretskii wrote:
>
> > My question is: what's the best way to use gcc dependency-generating
> > feature in a makefile? That's how I do it now:
> >
> > %.d: %.c
> > gcc -MM $< | sed ... >$@ #don't remember the exact command line,
> > #it inserts $@ between $*.o and ':'
> >
> > include foo.d bar.d ....
> >
> > It works, but when I start building from scratch make warns about lots
> > of non-existent .d files before generating them. I'd like to use more
> > elegant way if possible.
Eli's got some good suggestions here, but let me say that there is a
much nicer and cleaner way to handle dependency generation that I've
seen used in a number of GNU packages, including Emacs.
First, you have to remember that no matter what, the dependency
information won't be available until the second time you run the
makefile (unless you run Make recursively, that is). So any include
directives that specify nonexistent .d files is going to generate
warnings.
The way that is used by most GNU software, and that I have imported into
my own makefiles because I like it so much, is as follows:
1) Add -MD to the list of compiler options. This causes gcc to produce
a .d file for each source file automatically. There's no need to mess
around with sed scripts or anything messy like that.
2) Add the following bit to the end of your makefile. This is the best
part - it's extremely simple and elegant:
#
# Look for existing dependency files generated by -MD compiler option.
#
DEPS := $(wildcard *.d)
ifneq ($(DEPS),)
include $(DEPS)
endif
What this bit does is look for any .d files in the current directory,
and if it finds them it includes them in the makefile. It's as simple
as that. :)
--
John M. Aldrich, aka Fighteer I <fighteer AT cs DOT com>
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS d- s+:- a-->? c++>$ U@>++$ p>+ L>++ E>++ W++ N++ o+>++ K? w(---)
O- M-- V? PS+ PE Y+ PGP- t+(-) 5- X- R+(++) tv+() b+++ DI++ D++ G>++
e(*)>++++ h!() !r !y+()
------END GEEK CODE BLOCK------
- Raw text -