Auto header dependencies with gmake

EDITED

I am trying to recompile the source files without specifying header files for each CPP in the makefile.

I'm up to:

#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 obj/%.d @mkdir -p obj $(CXX) $(CXX_CFLAGS) -c $*.cpp -o $@ # Dependencies obj/%.d: %.cpp @mkdir -p obj $(CXX) $(CXX_CFLAGS) -MM -MF $@ $< DEPS = $(CORE_COMPONENT_OBJECTS:.o=.d) ifneq ($(MAKECMDGOALS),clean) -include $(DEPS) endif 

But changing header files does not initiate source files, including recompiling them.

NOTE: Actually, it works if my .o, .d and .cpp are in the same folder. But if my .d and .o are in the obj / folder, this does not cause recompilation.

+2
source share
3 answers

You do not have a dependency file as a prerequisite for a compilation rule. There 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 line adds a dependency on the headers.

EDIT

I see that you have

 -include $(DEPS) 

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

0
source

People often have rules for generating addiction, but they really aren't needed.

The first time a project is built, no dependencies are required, since it creates all sources anyway. Only subsequent builds require dependencies on the previous build in order to discover what needs to be rebuilt.

Therefore, dependencies are indeed a byproduct of compilation. Your rules should look like this:

 #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 not parallel, friendly. For example, when two or more processes are involved in creating /a/b/c/ and /a/b/cc/ , when /a/b/ does not exist, one mkdir process may fail with EEXIST trying to create /a/b/ .

+3
source

Better to use SCons/CMake/bjam to solve header dependency problems than to use make

-2
source