make include directive and dependency generation with -MM

后端 未结 3 1203
傲寒
傲寒 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 07:44

    The include directives work like they do in C and C++ - they are processed before anything else happens, to build the "real" makefile that make then processes. Specifically, they are processed before any rules are fired.

    0 讨论(0)
  • 2020-12-16 07:45

    An important point that it took me a while to grasp is that the make.deps from the previous build are good enough. Think about it: for a given object file, the only way the list of dependency files can change is if... one of the old dependency files has been altered. And if that's the case, then the old make.deps will cause that object file to be rebuilt, and if rebuilding the object file also rebuilds make.deps, then everything will be up to date. You don't have to rebuild make.deps before checking to see which objects must be rebuilt.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题