Chapter 3. Writing a make-rule to generate dependency information

In general it is much more convenient to let your makefiles generate their own dependency information instead of doing it by hand as described in the previous chapter.

Let's say we have a variable in our makefile that lists all source files, SOURCES. We also want to save dependency information to a file named .depend. Here is how to write a make rule to accomplish just that.

Example 3-1. Makefile fragment to generate dependencies from a number of sources

.depend:
	fastdep $(SOURCES) > .depend

-include .depend
	

It adds a new target .depend, which is file to hold the dependency information generated by fastdep for all files listed in SOURCES. The fragment also includes this file in the Makefile itself when it exist. If not make will generate it first (using the rule we specified), restart itself and include it next.

Now each time we want to regenerate dependencies, all we have to do is delete the .depend file and launch make.