I have written the following makefile:
CC=g++
all: happy
happy: happy.o HappyLetter.o
$(CC) -o happy happy.o HappyLetter.o
happy.o: happy.cpp
$(CC) -c happy.cpp
HappyLetter.o: HappyLetter.cpp
$(CC) -c HappyLetter.cpp
clean:
rm -rf *.o happy
and am working with the files HappyLetter.cpp and happy.cpp (which includes the former) to create an executable named happy.
I can build the code successfully using make. However, when I modify HappyLetter.cpp and type 'make' again, the change is not reflected. It only works when I type 'make clean' and then 'make'. The update of the object file that I expect to take place is echoed to the command line:
$ make
g++ -c HappyLetter.cpp
g++ -o happy happy.o HappyLetter.o
However, the update to HappyLetter.cpp is not being reflected in happy.
The problem does not work in the other direction. That is, if I modify happy.cpp, the change is reflected immediately after I type 'make'.
I have replicated this problem with three make binaries on my Mac OS X, and also on an Ubuntu machine. So I must be doing something wrong in the coding. Here is the text of the files, which are in the same directory as the makefile:
happy.cpp
#include "HappyLetter.cpp"
int main()
{
printf("Hello from happy.cpp!\n");
HappyLetter *myObj = new HappyLetter();
myObj->speak();
return 0;
}
HappyLetter.cpp
#include <cstdio>
class HappyLetter {
public:
void speak()
{
printf("Hello from HappyLetter.cpp!\n");
}
};
I believe the problem is something simple, but I have no more ideas about what to check. One assumption I have is that the ordering of the rules and dependencies does not matter.