From: "John M. Aldrich" Newsgroups: comp.os.msdos.djgpp Subject: Re: Another makefile question Date: Sun, 02 Mar 1997 15:56:21 -0800 Organization: Two pounds of chaos and a pinch of salt Lines: 57 Message-ID: <331A13A5.C1C@cs.com> References: Reply-To: fighteer AT cs DOT com NNTP-Posting-Host: ppp110.cs.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit CC: "Vyacheslav O. Myskin" To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp 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 -----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------