Chapter 4. Automatically regenerating dependency information

Every time you change a dependency relationship (for example when you include an extra header in some source file), you have to regenerate the dependency information manually by executing make .depend (as mentioned in the previous chapter deleting .depend also works)

Off course this is very error-prone. Why don't we let make regenerate the dependencies everytime one of the source files changes? Easy enough :

Example 4-1. Makefile fragment to generate dependencies each time a source file changes (wrong).

.depend: $(SOURCES)
	fastdep $(SOURCES) > .depend

-include .depend

This seems to work fine, but in fact doesn't. Suppose one (or more) of our source files in SOURCES includes a certain header foo.h. Now imagine that during an edit-compile cycle we don't change anything in the source files, but we added a new include of bar.h in foo.h. This means that the dependencies for each source file that includes foo.h must change. However since none of the source files themself change, make will find that .depend is still up to date and not regenerate it. The end result is incorrect dependency information.

To summarize, the dependency information of a source file not only depends on the source file itself, but also on all files it includes. Hence the earlier makefile fragment is incorrect.

To solve this problem, the info manual of GNU make proposes a solution where the output of the dependency generator is modified by piping it through sed. While this works for normal dependency generators like the GNU C/C++ compiler (and still works with fastdep), there is a much more elegant solution in fastdep. By adding the --remakedeptarget=file command line option fastdep will also emit a suitable dependency line for its own output.

Example 4-2. Makefile fragment to generate dependencies each time a source or included file changes (right).

	fastdep --remakedeptarget=.depend $(ALLSOURCES) > .depend

-include dependinfo

The .depend file will now look like this :

file1.o: \
	header1.h \
	header2.h \
file2.o: \
	header1.h \
	header3.h \
	header4.h \
.depend: \ \
	header1.h \
	header2.h \
	header3.h \ \
	header4.h \

which is exactly how we want it to be.