views:

115

answers:

3

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?

+2  A: 

Doesn't seem like it ought to happen. Perhaps you should check carefully for a spelling error. If your OS hides file extensions from you I'd be suspicious of that in particular.

dmckee
My golden rule for happy compiling - never, ever use mixed case file names.
anon
Or filenames or folders with spaces. Causes so many problems with command line parsing.
Adam Rosenfield
+2  A: 

I'm not able to reproduce your problem. You can try running make with the -d option to get a very detailed history of what make is trying to do. My best guess is that it's looking in the wrong directory for some reason.

Adam Rosenfield
A: 

I think that as no executable called target1 is generated, your target target1 will actually be a phony target and the corresponding commands will always be executed. So your code will be compiled in any case.
Although I'm also unable to reproduce your problem : No rule to make target `MyProg.cpp'

Neeraj