0

I want my makefile to be able to automatically create dependencies file and then decide whether a rebuild is needed.
While searching for a way to do it I found the following solution: I'm adding this line to my makefile:

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

and to the recipe for building .o files

%.o: %.cpp
    $(CC) $(CFLAGS) $(DEBUG) $<

I added the line

    $(CC) $(STD) -MM $< -o $*.d

If I understand correctly, every time an .o file doesn't exist, it ignores the include and build the .o file. Later, if I run make again, it checks the dependencies for this .o file (which he gets from the .d file) and decides whether it should rebuild the .o file. For that, once I build my project, I have to keep all these .d files.

What I want, is that every time make encounter the decision whether to rebuild or not an .o file, it will generate a .d file, specifically for this .o file. According to this .d file it will decide whether to rebuild, and delete the .d file when finished.

Is there a way I can achieve that?

I hope I expressed my will clear enough. If not, here's the flow I imagine:

if (.o file doesn't exist): build it
otherwise: build a .d file  
           rebuild or do nothing (according to the .d file)  
           delete the .d file
Jona
  • 669
  • 10
  • 18
  • Possible duplicate of [Makefile (Auto-Dependency Generation)](http://stackoverflow.com/questions/8025766/makefile-auto-dependency-generation). Get rid of the extra line and use `-MMD -MP` instead. – user657267 Sep 20 '16 at 11:36

1 Answers1

0

The behavior you want is how make already works, except for the deleting of the .d file. I don't know why you want that: it means that every time make runs it will have to rebuild the .d file before it can tell if an object file is out of date: this will increase your build times dramatically for no good reason.

Every time make rebuilds any included makefile, it will re-exec itself and start reading makefiles from scratch. So as long as you don't delete the .d file it will work as you desire. Information about this feature is in the GNU make manual.

However, this is a sub-optimal way of managing dependencies. It works but it is slower than it needs to be. An explanation of a more "modern" way to handle this can be found here.

MadScientist
  • 92,819
  • 9
  • 109
  • 136