View Full Version : Is there anything similar to g++ -MM that doesn't strip off directories?

July 1st, 2009, 07:23 AM
I'm trying to set my project up to have automatically generated dependencies. I'm using the old fashioned approach with just one depend file. The project has source files in several subdirectories, so if I just use g++ -MM, a source file like "subdir/foo.cpp" generates a rule for "foo.o" in my depend file. This is bad, to match "subdir/foo.cpp" it needs to "subdir/foo.o". Is there a different version of the -MM flag that doesn't strip off subdirs? I've been reading through the gcc manual and see all sorts of things for putting different constants before the object file name but haven't found anything that does quite what I want.

July 1st, 2009, 01:19 PM
If you have source code in one or more sub-directories of a project, here is one way to build the application (and the dependencies):

APP = myapp

OBJDIR = .srcobjs

SRCS := $(shell find . -name '*.cpp')
SRCDIRS := $(shell find . -name '*.cpp' -exec dirname {} \; | uniq)
OBJS := $(patsubst %.cpp,$(OBJDIR)/%.o,$(SRCS))
DEPS = $(patsubst %.cpp,$(OBJDIR)/%.d,$(SRCS))

INCLUDES = -I./Include
CXXFLAGS = -Wall -pedantic -ansi -c $(INCLUDES)

all: $(APP)

$(APP) : buildrepo $(OBJS)
@echo "Building $@..."
@$(CXX) $(OBJS) $(LDFLAGS) -o $@

$(OBJDIR)/%.o: %.cpp
@echo "Generating dependencies for $<..."
@$(call make-depend,$<,$@,$(subst .o,.d,$@))
@echo "Compiling $<..."
@$(CXX) $(CXXFLAGS) $< -o $@

$(RM) -r $(OBJDIR)

distclean: clean
$(RM) $(APP)

@$(call make-repo)

define make-repo
for dir in $(SRCDIRS); \
do \
mkdir -p $(OBJDIR)/$$dir; \

# usage: $(call make-depend,source-file,object-file,depend-file)
define make-depend
$(CXX) -MM \
-MF $3 \
-MP \
-MT $2 \

ifneq "$(MAKECMDGOALS)" "clean"
-include $(DEPS)

I've attached a sample project that employs the Makefile shown above. You can give it a try for test purposes.


EDITED: Oops! I forgot to add the appropriate Makefile statement to include the dependencies. That has been corrected now.

July 1st, 2009, 11:06 PM
Wow, I didn't expect such a detailed reply! Thanks!