Michel Billaud
Michel Billaud

Reputation: 1826

Building dependencies by a recursive call to make?

I've read some articles explaining how recursive makefiles were evil in the case of compiling projects with subdirectories.

But, I found this handy way to automatically generate dependencies

exec = main
objs  = main.o A.o B.o C.o   # main and object modules

deps = $(objs:.o=.d)         # dependencies file

all: $(deps)
    $(MAKE) $(exec)

-include $(deps)

%.d: %.c                        # how to build dependency files
    $(CC) -MM $(CFLAGS) $< > $@

main: $(objs)          # How to build the main exec

Building the target "all" updates the dependency files if needed, and then considers rebuilding the main exec if needed.

Is there something fundamentally flawed with this approach?

Upvotes: 1

Views: 213

Answers (2)

Michel Billaud
Michel Billaud

Reputation: 1826

As remarked by other contributors, gnu make treats .d dependencies files as Makefile and rebuilds them automagically when needed.

This makes the recursive call unnecessary, and the question falls flat.

Of course, as usual, "considered harmful" paper titles have to be read with a grain of salt, if really read at all, from a long tradition (Knuth's "rebuttal" of Dijkstra's paper cites Disjktra own fear to be believed as terribly dogmatic" and "others making a religion of it". "Fanatical advocates going over the board" !).

Upvotes: 0

Thomas
Thomas

Reputation: 181705

If you're using GNU make, this hack is unnecessary. GNU make is smart enough to automatically rebuild any (make)file that is included, and then restart itself:

Since the ‘.d’ files are makefiles like any others, make will remake them as necessary with no further work from you. See Remaking Makefiles.

About other make implementations, the GNU make manual has this to say:

With old make programs, it was traditional practice to use [the -M] compiler feature to generate prerequisites on demand with a command like ‘make depend’. That command would create a file depend containing all the automatically-generated prerequisites; then the makefile could use include to read them in (see Include).

Upvotes: 4

Related Questions