Automatic header dependencies with gmake

前端 未结 3 1891
一个人的身影
一个人的身影 2020-12-04 00:53

EDITED

I\'m trying to have source files recompiled without having to specify header files for each CPP in the makefile.

I\'m down to :

相关标签:
3条回答
  • 2020-12-04 01:10

    You don't have dependency file as a prerequisite for compilation rule. Should be something like this:

    #This is the rule for creating the dependency files
    src/%.d: src/%.cpp
        $(CXX) $(CXX_CFLAGS) -MM -MF $(patsubst obj/%.o,obj/%.d,$@) -o $@ $<
    
    obj/%.o: %.cpp %.d
        $(CXX) $(CXXFLAGS) -o $@ -c $<
    
    -include $(SRC:%.cpp=%.d)
    

    The last string adds dependency on headers.

    EDIT

    I see you have

    -include $(DEPS)
    

    but check with $(warning DEPS = $(DEPS)) whether you really include existing files, otherwise make just silently ignore these.

    0 讨论(0)
  • 2020-12-04 01:18

    People often have such rules for dependency generation, but they are really unnecessary.

    The first time a project is built no dependencies are necessary since it builds all sources anyway. It is only the subsequent builds that require the dependencies from the previous build to detect what needs to be rebuild.

    Therefore, the dependencies are really a by-product of compilation. Your rules should look like the following:

    #CoreObj1.cpp(and .h)
    #CoreObj2.cpp(and .h)
    
    #This is the makefile.
    
    CORE_COMPONENT_OBJECTS = \
      obj/CoreObj1.o \
      obj/CoreObj2.o \
    
    # Objects
    obj/%.o: %.cpp
            @mkdir -p obj
            $(CXX) $(CXX_CFLAGS) -c -o $@ -MD -MP -MF ${@:.o=.d} $<
    
    DEPS = $(CORE_COMPONENT_OBJECTS:.o=.d)
    
    ifneq ($(MAKECMDGOALS),clean)
    -include $(DEPS)
    endif   
    

    As a side note, mkdir -p is not parallel make friendly. For example, when two or more processes race to create /a/b/c/ and /a/b/cc/ when /a/b/ doesn't exist, one mkdir process may fail with EEXIST trying to create /a/b/.

    0 讨论(0)
  • 2020-12-04 01:27

    It's better to use SCons/CMake/bjam to solve "header dependencies problem" than use make

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