15

I'm using gmake and gcc -MM to track header dependencies, following the manual. The mechanism relies on a makefile include directive to import the computed dependencies.

Because the .d files are included by the makefile, they must exist for any target to be made, including clean. So before clean can do the right thing, the dependencies must be generated, and if one fails to build then clean just made more mess.

Besides clean, it wants to make all the dependencies before building any target.

Furthermore, if any file is changed to include a nonexistent file, then the dependency resolution breaks and nothing at all will build.

If a header is deleted, then the existing dependency files contain still name it as a target, and nothing will build until the offending dependency files are removed… which can't be done with clean.

Replacing the substitution pattern of the include with a wildcard to include all preexisting dependency files solves some of the issues, but it still can't clean out a broken dependency, and stale dependency files are never removed. Is there a better solution? Is the manual's example really intended for real use?

3 Answers 3

19

Just don't provide a rule for generating .d files. A good explanation of why it is not so good (including your case too) is available in "Advanced Auto-Dependency Generation" of Paul Smith - a maintainer of GNU Make.

In a nutshell, the following pattern works fine for me for all cases:

CPPFLAGS += -MMD -MP

%.o: %.c
    $(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $<

-include $(OBJS:.o=.d)

See also my previous related answers:

19
  • @Potatoswatter, no. In this case generation of .d files is combined with the actual compilation, GCC performs these steps within a single invocation. Oct 12, 2012 at 11:01
  • Sorry, I shouldn't have spoken before reading a little more. It looks like -MP solves the remaining issue after what sehe solved. But I prefer to keep objects in another directory from sources, and might want more processing of the dependency files to support precompiled headers. Oct 12, 2012 at 11:05
  • @Potatoswatter, I have never used PCH before, however splitting directories for objects and sources isn't a big problem. Just prefix target and prerequisite with paths: $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c. Dependency files will be placed just near the object files, in $(OBJ_DIR). Oct 12, 2012 at 11:13
  • 1
    One tweak of this admirable method: If someone deletes f.d say, and then edits f.h, then make will not recompile f.o (as no one has told it f.o: f.h). In this case you can get make to delete any .o files that do not have a corresponding .d file. Add this to the end of the makefile: ${OBJS:.o=.d}: ; rm ${@:.d=.o}
    – bobbogo
    Oct 12, 2012 at 12:22
  • 1
    @Daniel Parallel programming is one area where you need a strong grip of what is going on. There is no substitute for this.
    – bobbogo
    Jun 12, 2015 at 10:52
10

The solution is to use Conditional Syntax:

ifneq ($(MAKECMDGOALS), clean)
-include $(notdir $(SOURCES:.cpp=.d))
endif

This makes the clean target do not invoke the *.d targets, since when you run make clean the *.d files will not be included in the Makefile.

Reference: https://www.gnu.org/software/make/manual/html_node/Goals.html

2

My usual pattern looks like

all: target
target: .depends

## [snip build rules]

.depends:
     gcc -MM $(CPPFLAGS) .... > $@

-include .depends

Note -include instead of include. Basically, it includes conditionally: i.e. iff file exists

See documentation: http://www.gnu.org/software/make/manual/make.html#Include

3
  • +1: adding the minus sign fixed the immediate problem. Thanks, now I'm pointed in the right direction… Oct 12, 2012 at 10:58
  • 4
    It is sub-optimal and unnecessary to have a rule to generate dependencies these days. Dependencies are better generated as a by-product of compilation. For the first compilation dependencies are unnecessary since every source needs to be built. Only for subsequent builds dependencies are needed and they have been produced by a previous build. Oct 12, 2012 at 11:01
  • @MaximYegorushkin I learn everyday. Nice. (I do read the other answers too :))
    – sehe
    Oct 12, 2012 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.