make include directive and dependency generation with -MM

后端 未结 3 1210
傲寒
傲寒 2020-12-16 07:07

I want a build rule to be triggered by an include directive if the target of the include is out of date or doesn\'t exist.

Currently the makefile looks like this:

3条回答
  •  独厮守ぢ
    2020-12-16 08:00

    You are relying on an implicit rule to compile your .cpp files. You have to redefine it to use the -MM and -MF flags that will create the dependency file.

    %.o: %.cpp
        $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@ -MM -MF $@.d
    

    Then, you have to include these dependencies files in the Makefile, using -include that will not error when the dependencies files do not exist yet (on the first time, or after a clean).

    program_DEPS := $(program_OBJS:.o=.o.d)
    -include $(program_DEPS)
    

    And remember to add the rm command for the dependencies files in the clean rule.

提交回复
热议问题