4 ms·
There's generally a much nicer way to handle this so that the dependencies don't have to get generated before the first compile (and are instead generated as a
by Arch-TK 3y ago
There's generally a much nicer way to handle this so that the dependencies don't have to get generated before the first compile (and are instead generated as a side effect of the first compile, which is sufficient as the first build will always be complete and you only need granular dependency information for proper incremental rebuilds).
CFLAGS += -MD
-include $(wildcard *.d)
I personally like to also pass `-MP` to add phony targets so removing a header doesn't complain about headers which are intentionally removed.
You can pass `-MMD` instead of `-MD` to avoid having system includes in the generated depfiles just like the example you wrote does.
I also personally prefer to be explicit and avoid wildcards in makefiles wherever possible so I would do something like this:
CFLAGS += -MD -MP
objs := foo.o bar.o ... baz.o
deps = $(objs:.o=.d)
...
-include $(deps)
That being said, this doesn't solve the "everything should depend on the Makefile" problem as well as the "what if I generate a generate source code" problem or finally "what if I have to compile C to generate C to compile my program" problem.
As such, I have personally switched to redo.