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