Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling a single c++ file with makefile using -g flag doesn't work

Tags:

c++

makefile

g++

I have been trying to use Makefile to be able to compile a single cpp file and then easily clean the generated executable. However, the Makefile code shown below doesn't compile the code with -g. As a result, I cannot use gdb on the executable. I am able to compile the code with -g flag successfully if I compile it using "g++ -g pairs_with_k_diff.cpp" in the command line. Would someone be kind enough to point out what my mistake is in the Makefile code shown below?

pawkdiff: pairs_with_k_diff.o
    g++ pairs_with_k_diff.o -o pawkdiff

pawkdiff.o: pairs_with_k_diff.cpp
    g++ -c -g pairs_with_k_diff.cpp

clean:
    rm *.o pawkdiff

When I call make, I get the following output (As you can see, the -g flag is absent).

g++    -c -o pairs_with_k_diff.o pairs_with_k_diff.cpp
g++ pairs_with_k_diff.o -o pawkdiff
like image 663
Anil Celik Maral Avatar asked Jan 20 '26 06:01

Anil Celik Maral


1 Answers

You have no rule for making pairs_with_k_diff.o, so the default rule is used.

Fix your second rule to make it work:

pairs_with_k_diff.o: pairs_with_k_diff.cpp
    g++ -c -g pairs_with_k_diff.cpp

Or as an alternative:

pawkdiff: pawkdiff.o
    g++ pairs_with_k_diff.o -o pawkdiff

pawkdiff.o: pairs_with_k_diff.cpp
    g++ -c -g pairs_with_k_diff.cpp -o pawkdiff.o
like image 183
Mat Avatar answered Jan 22 '26 19:01

Mat