listerreg
listerreg

Reputation: 644

MAKEFILE: treating target as updated without modifying the file

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

Upvotes: 1

Views: 773

Answers (2)

Barry
Barry

Reputation: 303186

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!

Upvotes: 4

Maksim Solovjov
Maksim Solovjov

Reputation: 3157

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.

Upvotes: 1

Related Questions