Note: using MinGW’s make (should be GNU make)
i have a couple of -include statements in my makefile to import dependencies which were generated using g++ -MM. However I would like to only do this when necessary. I have several different build targets and I don’t want all of their respective dependency files to be included since this takes a while (suppose I’m running make clean: no need to include them in this case)
Here’s the format of my makefile.
DEPS_debug = $(patsubst %.cpp,build_debug/%.d,$(SRC))
OBJ_debug = $(patsubst %.cpp,build_debug/%.o,$(SRC))
all: program_debug
-include $(DEPS_debug) #make: include: Command not found
program_debug: $(OBJ_debug)
$(CC) $(CFLAGS) $(OBJ_debug) -o $@
If you really don’t want to include those files needlessly, you have a couple of options:
You can put in a conditional as Diego Sevilla suggests (but I would recommend using
MAKECMDGOALSso that you can write a more flexible version, specific to targets, e.g. you’ll includefoo.dif and only if you’re makingfoo.o).You can use make recursively (heresy!), invoking
$(MAKE)for each target object, using a makefile that includes that target’s dependencies.But actually including the file takes negligible time, it’s the rebuilding of the file (automatic for any included file that’s out of date) that takes time.
If needless rebuilding is what you want to avoid, you can use a very clever trick. When must
foo.dbe rebuilt? Only when something aboutfoohas changed. But in that casefoo.omust also be rebuilt. So don’t have a seperate rule forfoo.d, just rebuild it as a side effect of makingfoo.o. That way you can include all dependency files and not waste time rebuilding them if they aren’t needed.EDIT:
I’m astounded that merely including these files can add 2-3 seconds to
make clean. My last paragraph is off the mark, so let me expand on the first two options.If
allis the only target for which these files should be included, and youmake allfrom the command line (and not e.g.make all tests tarball install kitchenSink), then this will do it:Note that this will not include
foo.dif youmake foo.o. You can write a more sophisticated conditional, something likebut that’s pretty advanced, so let’s get a simple version working first.
If you’d rather avoid the conditional and use recursive Make, the simplest way is to split the makefile in two:
makefile:
makefile.all: