0

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?

asked Aug 2, 2009 at 19:33

3 Answers 3

2

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.

answered Aug 2, 2009 at 19:38
Sign up to request clarification or add additional context in comments.

2 Comments

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

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.

answered Aug 2, 2009 at 19:39

Comments

0

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'

answered Aug 2, 2009 at 20:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.