[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: adding *.h dependency, problem
From: |
Maxim Yegorushkin |
Subject: |
Re: adding *.h dependency, problem |
Date: |
Fri, 17 Aug 2007 15:31:18 +0100 |
User-agent: |
Thunderbird 2.0.0.5 (X11/20070719) |
Yakov Lerner wrote:
I have tiny makefile that looks like this:
all: prog
prog: a.o b.o ; $(CC) -o $@ a.o b.o
It is very short because it relies on builtin %.o: %.c rule.
How do I add the following dependency: that all *.c files depend on all *.h ?
So that when any *.h changes, all *.o are recompiled ?
I tried to add '%.o: %.c *.h' now, but it didn't do the trick.
A better idea may be to generate dependencies automatically from the source
files. As many compilers can generate a make dependency file from #include
directives in the source, the dependencies can be created as a by-product of
compilation and then included in make on the next invocation. The idea, as
described in http://make.paulandlesley.org/autodep.html (the site seem to be not
responding now), to have the first invocation of make (after make clean)
generate the dependencies while compiling everything, so that the next
invocations will only recompile sources that have changed or their dependencies
have changed and also update the dependency file for the source being recompiled.
Here is a working prototype:
address@hidden autodep]$ cat foo.c
#include "a.h"
#include "b.h"
int main() { return 0; }
address@hidden autodep]$ cat bar.c
#include "b.h"
#include "c.h"
int main() {}
address@hidden autodep]$ cat Makefile
all :
CC := gcc
# bin targets to build
bin_targets =
bin_targets += foo
foo_obj := foo.o
bin_targets += bar
bar_obj := bar.o
.SECONDEXPANSION:
all : $(bin_targets)
$(bin_targets) : $$(address@hidden)
$(CC) -o $@ $^
# produce .d dependencies as a by-product of compilation
# -MMD for gcc, -xMMD for Sun Studio 12 CC
%.o : %.c
$(CC) -c -o $@ -MMD $<
.PHONY: all clean
clean :
rm -f *.d *.o
# include autogenerated dependencies if any
all_objs := ${foreach bin,$(bin_targets),$($(bin)_obj)}
all_deps := $(all_objs:%.o=%.d)
-include $(all_deps)
address@hidden autodep]$ touch a.h b.h c.h
address@hidden autodep]$ make
gcc -c -o foo.o -MMD foo.c
gcc -o foo foo.o
gcc -c -o bar.o -MMD bar.c
gcc -o bar bar.o
address@hidden autodep]$ make
make: Nothing to be done for `all'.
address@hidden autodep]$ touch a.h
address@hidden autodep]$ make
gcc -c -o foo.o -MMD foo.c
gcc -o foo foo.o
address@hidden autodep]$ make
make: Nothing to be done for `all'.
address@hidden autodep]$ touch b.h
address@hidden autodep]$ make
gcc -c -o foo.o -MMD foo.c
gcc -o foo foo.o
gcc -c -o bar.o -MMD bar.c
gcc -o bar bar.o
address@hidden autodep]$ make
make: Nothing to be done for `all'.
address@hidden autodep]$ touch c.h
address@hidden autodep]$ make
gcc -c -o bar.o -MMD bar.c
gcc -o bar bar.o
address@hidden autodep]$ make
make: Nothing to be done for `all'.