I’ve got a common set of make rules being used in multiple directories. Each make is very simple:
BASEDIR:=../..
TARGET:=theTarget
include Makefile.include
Unless overridden, I have a default value for the source code:
SRC:= $(wildcard *.cpp)
We want to build the object files in a directory (not with the sources), so:
OBJDIR:= ../obj
BARE:= $(foreach f,$(SRC),$(notdir $(f) )
OBJ:= $(foreach o,$(BARE),$(OBJDIR)/$(o).o )
So I have the .o files, but I need each one to fire a rule based on the .cpp
As long as the directory is known, this is easy:
$(OBJDIR)/%.o: %.cpp
but in some of the directories, there are arbitrarily grouped files in subdirectories. I temporarily got around this by hardcoding all of them in my central makefile:
$(OBJDIR)/%.o: a/%.cpp
build...
$(OBJDIR)/%.o: b/%.cpp
build...
But what I’d like to do is be able to specify the build directories in a variable if possible. Is there any way to set up the dependencies
$(OBJDIR)/x.o: a/x.cpp
$(OBJDIR)/y.o: a/y.cpp
and
$(OBJDIR)/z.o: b/z.cpp
without manually having to list out the dependencies?
For that matter, I also have dependency files (.d)
$(DEPDIR)/%.d: $(SRC)
I want to set up dependencies based on whatever is in the source.
For example:
.deps/x.d: a/x.cpp
g++ -MM -MT$(objdir)/$(notdir $@) $< -o $(DEPDIR)/$(notdir $@ )
Is there an easy way to generate these rules without writing them for each directory?
Here’s where I got the information on generating the .d files in the first place:
http://mad-scientist.net/make/autodep.html#advanced
The goal in the end is to include all the .d files and have dependence tree auto-generated from the files. But that is the last part. I don’t know how to use the .d files. Do I just include them all?
include $(DEPDIR)/%.d
doesn’t work.
I can cat them all together in a rule:
cat $(DEP) >$(DEPDIR)/deps.inc
and then
include deps.inc
First, to cope with the source directories:
Then if you want you can generate the dependency files like this:
But there is a better way which is described in the source you cite. Don’t make a rule for the
.dfiles, just build them in the same rule that builds the object files. That way you don’t build them unless you actually need them.