1

Here's a simplified version of my Makefile:

all: myprogram

myprogram: main.o
    c++ main.o -o myprogram

main.o: main.cpp mylib.hpp
    c++ -c main.cpp 

mylib.hpp: mylib.inl

All these files mentioned above are real files. When I change mylib.hpp, main.cpp recompiles. However, my problem is that when I change mylib.inl, main.cpp does not recompile. How can I invalidate the main.o target and therefore the myprogram target when I edit mylib.inl? I would prefer not to use a .PHONY target, because I don't want to recompile everything every time, just only when I edit mylib.inl.

Jerfov2
  • 5,264
  • 5
  • 30
  • 52

1 Answers1

4

Your problem is that the dependency described in your makefile:

mylib.hpp: mylib.inl

does not describe a real dependency. Changing mylib.inl does not change mylib.hpp (there is no rule to trigger such a change).

If mylib.inl is included from mylib.hpp then it should actually be a dependancy on main.o rather than mylib.hpp, leading to a dependency like this:

main.o: main.cpp mylib.hpp mylib.inl

Maintaining individual dependencies manually is likely to be error prone, which is why most compilers provide functions that can auto-create dependency files that you can include from your make file. You can then supply more generic rules for compilation. For more details on how to use this, check out this article.

harmic
  • 28,606
  • 5
  • 67
  • 91