I have a makefile, and a snippet looks like this (files and targets renamed for clarity):
target1 : OtherLib.o MyProg.h MyProg.cpp g++ -c ./MyProg.cpp -o $(BUILD_DIR)/MyProg.o
When I run 'make target1', I want make to check to see that MyProg.h and MyProg.cpp are up to date and run target1 if not. However, I am getting the error
make: *** No rule to make target `MyProg.cpp', needed by `target1'. Stop.
But 'MyProg.cpp' is what I'm trying to compile! So, I made an empty target for it, like this:
MyProg.h : MyProg.cpp : target1 : OtherLib.o MyProg.h MyProg.cpp g++ -c ./MyProg.cpp -o $(BUILD_DIR)/MyProg.o
This works, but it seems weird. I am under the impression that if I do simply this:
target1 : OtherLib.o g++ -c ./MyProg.cpp -o $(BUILD_DIR)/MyProg.o
then MyProg will be compiled regardless of whether or not it is up to date, because MyProg.cpp is not listed as a dependency. What is the right way to do this so that make will compile MyProg.o only if MyProg.cpp and MyProg.h are not up to date?