1

How can I make GNU make to treat a target as modified without modifying target file (no recipe body)?

I have header file which includes another header file.

program.h:

//include guard    

#include "dependencies.h"

//some actual signatures

and makefile:

program: program.o dependencies.o
  g++ -o program program.o dependencies.o

program.o: program.cpp program.h
  g++ -c program.cpp

dependencies.o: dependencies.cpp dependencies.h
  g++ -c dependencies.cpp

program.h: dependencies.h
  # I would like that this target was treated as modified after
  # dependencies.h changes

In the above example when dependencies.h changes only dependencies.o is recompiled leaving program.o untouched and thus the build will probably fail. Is it possible to change this behavior?

Thanks.

Alek

listerreg
  • 584
  • 1
  • 6
  • 15
  • Your question in general is similar to: https://stackoverflow.com/questions/2394609/makefile-header-dependencies . Here is another example: http://scottmcpeak.com/autodepend/autodepend.html – Alex Lop. Aug 18 '15 at 18:31

2 Answers2

4

Change these lines:

program.o: program.cpp program.h
  g++ -c program.cpp

dependencies.o: dependencies.cpp dependencies.h
  g++ -c dependencies.cpp

To be:

OBJ_FILES = program.o dependencies.o
DEP_FILES = $(patsbust %.o,%.d,$(OBJ_FILES))

%.o : %.cpp
    g++ -c $< -MP -MMD -MF $(@:.o=.d) -o $@

program : $(OBJ_FILES)
    g++ -o $@ $^

-include $(DEP_FILES)

This will have g++ autogenerate your dependency files for you (into the .d) files. Those files will have makefile-style rules for dependencies. In this case, I'd expect:

program.o : dependencies.h

The -include will add those to your makefile. That way, you don't have to hardcode your dependencies - let the compiler figure it out for you!

Barry
  • 286,269
  • 29
  • 621
  • 977
1

Make your program depend on its dependencies in full:

program.o: program.cpp program.h dependencies.h
  g++ -c program.cpp

This is of course given that your program.o actually depends on dependencies.h. If dependencies.h is an internal implementation detail of program.h, then your code should compile and link just fine without recompiling program.o.

Maksim Solovjov
  • 3,147
  • 18
  • 28
  • I'm doing it exactly like you say but this is tedious if there are more chained dependencies (and they are not an implementation detail but are part of a signatures f.e.). I'm left with lines like this: `main.o: myWindows.h myViewModels.h iMyView.h model.h $(DALINCLUDEDIR)/$(DALHEADER) $(GUIINCLUDEDIR)/$(GUIHEADER) $(SERVERINCLUDEDIR)/$(SERVERHEADER)` – listerreg Aug 18 '15 at 17:00
  • Then maybe Barry's automated solution will help :) – Maksim Solovjov Aug 18 '15 at 17:02