generate dependencies for a makefile for a project in C/C++ generate dependencies for a makefile for a project in C/C++ c c

generate dependencies for a makefile for a project in C/C++


GNU make's documentation provides a good solution.

Absolutely. g++ -MM <your file> will generate a GMake compatible list of dependencies. I use something like this:

# Add .d to Make's recognized suffixes.SUFFIXES += .d#We don't need to clean up when we're making these targetsNODEPS:=clean tags svn#Find all the C++ files in the src/ directorySOURCES:=$(shell find src/ -name "*.cpp")#These are the dependency files, which make will clean up after it creates themDEPFILES:=$(patsubst %.cpp,%.d,$(SOURCES))#Don't create dependencies when we're cleaning, for instanceifeq (0, $(words $(findstring $(MAKECMDGOALS), $(NODEPS))))    #Chances are, these files don't exist.  GMake will create them and    #clean up automatically afterwards    -include $(DEPFILES)endif#This is the rule for creating the dependency filessrc/%.d: src/%.cpp    $(CXX) $(CXXFLAGS) -MM -MT '$(patsubst src/%.cpp,obj/%.o,$<)' $< -MF $@#This rule does the compilationobj/%.o: src/%.cpp src/%.d src/%.h    @$(MKDIR) $(dir $@)    $(CXX) $(CXXFLAGS) -o $@ -c $<

Note: $(CXX)/gcc command must be preceded with a hard tab

What this will do is automatically generate the dependencies for each file that has changed, and compile them according to whatever rule you have in place. This allows me to just dump new files into the src/ directory, and have them compiled automatically, dependencies and all.


Having now read this portion in particular I think there is a much easier solution out there, as long as you have a reasonably up to date version of gcc/g++. If you just add -MMD to your CFLAGS, define a variable OBJS representing all your object files, and then do:

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

then that should get you both an efficient and simple automatic dependency build system.


The GNU C preprocessor cpp has an option, -MM, which produces a make-suitable set of dependencies based on inclusion patterns.