Let's say I have a makefile with the rule
%.o: %.c
gcc -Wall -Iinclude ...
I want *.o to be rebuilt whenever a header file changes. Rather than work out a list of dependencies, whenever any header file in /include
changes, then all objects in the dir must be rebuilt.
I can't think of a nice way to change the rule to accomodate this, I'm open to suggestions. Bonus points if the list of headers doesn't have to be hard-coded
Here's a two-liner:
This works with the default make recipe, as long as you have a list of all your object files in
OBJS
.If you are using a GNU compiler, the compiler can assemble a list of dependencies for you. Makefile fragment:
or
where
SRCS
is a variable pointing to your entire list of source files.There is also the tool
makedepend
, but I never liked it as much asgcc -MM
How about something like:
You could also use the wildcards directly, but I tend to find I need them in more than one place.
Note that this only works well on small projects, since it assumes that every object file depends on every header file.
This will do the job just fine , and even handle subdirs being specified:
tested it with gcc 4.8.3