I wrote this simple makefile, which works fine until I want to do some changes and apply them into binary. Then I need to do 'make clean' and 'make' again, because obj. file will not be rebuilt. Althought it seems like a simple task so far I failed to modify it in order to make obj. file rebuild implicitly (So I can write only 'make' when I modify source).
COMPILER=g++-5.3
CXXFLAGS=-std=c++11 -O2 -pedantic
build : a.o
$(COMPILER) a.o -o a
a.o:
$(COMPILER) $(CXXFLAGS) -c a.cpp -o a.o
clean :
rm -f *.o *~ a
Edit: I tried changes suggested by Omada (I tried the same myself before I wrote this question) and this is result:
me@pc: ~/mypath$ cat a.cpp
#include <iostream>
int main(int argc, char* argv[])
{
if ( argc!=2 ){
std::ceaaarr<<"Prrogram expects 1 argument\n";
return 1;
}
return 0;
}me@pc: ~/mypath$ make
g++-5.3 a.o -o a
me@pc: ~/mypath$ cat Makefile
#init
COMPILER=g++-5.3
CXXFLAGS=-std=c++11 -O2 -pedantic
build: a.o
$(COMPILER) a.o -o a
a.o: a.cpp
$(COMPILER) $(CXXFLAGS) -c a.cpp -o a.o
clean:
rm -f *.o *~ a
Since you never told the makefile that a.o depends on a.cpp, it won't be able to determine that when a.cpp changes, it should rebuild a.o.
To fix this change
a.o:
$(COMPILER) $(CXXFLAGS) -c a.cpp -o a.o
to this
a.o: a.cpp
$(COMPILER) $(CXXFLAGS) -c a.cpp -o a.o
Let me know if it doesn't work, I'm a bit rusty on my makefiles.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With