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