I renovated some makefiles this week and found myself wanting to generate dependencies of a piece of code automatically from ‘gcc -M’. The existing Makefile had an explicit “make depends” step that would invoke ‘gcc -M’ for each of the source files. This is annoying because all generated Makefiles would have to be recreated if one file’s dependencies changed.

The GNU Make Manual has a section on automatically generating prerequisites, which only gets you part of the way.

I owe a good deal of gratitude to Tom Tromey and Paul D. Smith for creating and writing up instructions on how GNU Make can automatically generate dependencies for source files. They discuss even better ways of generating dependency files and build upon the GNU Make Manual method.