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:
[EMAIL PROTECTED] autodep]$ cat foo.c
#include "a.h"
#include "b.h"
int main() { return 0; }
[EMAIL PROTECTED] autodep]$ cat bar.c
#include "b.h"
#include "c.h"
int main() {}
[EMAIL PROTECTED] 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) : $$([EMAIL PROTECTED])
$(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)
[EMAIL PROTECTED] autodep]$ touch a.h b.h c.h
[EMAIL PROTECTED] 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
[EMAIL PROTECTED] autodep]$ make
make: Nothing to be done for `all'.
[EMAIL PROTECTED] autodep]$ touch a.h
[EMAIL PROTECTED] autodep]$ make
gcc -c -o foo.o -MMD foo.c
gcc -o foo foo.o
[EMAIL PROTECTED] autodep]$ make
make: Nothing to be done for `all'.
[EMAIL PROTECTED] autodep]$ touch b.h
[EMAIL PROTECTED] 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
[EMAIL PROTECTED] autodep]$ make
make: Nothing to be done for `all'.
[EMAIL PROTECTED] autodep]$ touch c.h
[EMAIL PROTECTED] autodep]$ make
gcc -c -o bar.o -MMD bar.c
gcc -o bar bar.o
[EMAIL PROTECTED] autodep]$ make
make: Nothing to be done for `all'.
_______________________________________________
Help-make mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/help-make