You are not logged in.

#1 2018-02-11 15:25:11

JohnBobSmith
Member
From: Canada
Registered: 2014-11-29
Posts: 759

[SOLVED]g++ -g in makefile does not produce debugging symbols?

Hello,

I'm experimenting with using a debugger, specifically gdb, to debug my c++ programs instead of using other methods like printing my variables manually. Unfortunately I can't produce debugging symbols from my makefile. I have the following main.cpp:

#include <iostream>

int main()
{
	int one = 1;
	one += 3;
	std::cout << "Compiler, y u no produce debugging symbols?!\n";
}

And the following makefile:

CXX = g++
CXXFLAGS = -g -Wall
PROG  = debugging_tests
OBJS = main.o

all: $(PROG)

%.o: %.cpp
	$(CXX) -c -o $@ $<

$(PROG): $(OBJS)
	$(CXX) -o $(PROG) $^ $(CXXFLAGS)

clean:
	rm -f *.o $(PROG)

And now, for the experiment. Its a little bit long so I posted it to github: https://gist.github.com/JohnBobSmith/1a … dc69684eaa
Heres the relevant breakdown of what I did and why:
-Clean, then build debugging_tests from my makefile
-Run it in gdb via gdb debugging_tests
-Notice on line 23 of the gist, no debugging symbols found.
-Trying to use break yields the no symbol table found error.
-Clean and build main.cpp directly using g++
-run gdb a.out
-notice on line 48 of the gist gdb read the symbols
-I can successfully add a breakpoint.

My question is: Why can't I build debugging symbols from my makefile with -g? It is present in my CXXFLAGS. I have tried moving the order of things around with no success. I've tried directly typing the command in instead of using variables, but nope. I'm not sure why make wont produce debugging symbols. Hopefully I'm not missing the obvious. Thanks for the help! smile

Last edited by JohnBobSmith (2018-02-11 15:43:44)


I have been diagnosed with Bipolar Disorder. As it turns out what I thought was once my greatest enemy is now my greatest strength.

Offline

#2 2018-02-11 15:34:37

ayekat
Member
Registered: 2011-01-17
Posts: 1,220

Re: [SOLVED]g++ -g in makefile does not produce debugging symbols?

I haven't looked at the gist yet, but the CXXFLAGS should be used for compiling the sources into the object (cpp → o), not for linking (o → a.out).

Offline

#3 2018-02-11 15:36:13

Trilby
Forum Fellow
Registered: 2011-11-29
Posts: 18,324
Website

Re: [SOLVED]g++ -g in makefile does not produce debugging symbols?

One obvious difference is in your makefile you aren't actually using CXXFLAGS for the compile step, only for linking.  If you just let make use a default recipe it will include it for you.  If you write your own recipe you need to specify CXXFLAGS.

Note that when you built directly on the command line, you did include your CXXFLAGS content.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2018-02-11 15:42:10

JohnBobSmith
Member
From: Canada
Registered: 2014-11-29
Posts: 759

Re: [SOLVED]g++ -g in makefile does not produce debugging symbols?

Thanks ayekat. Moving my CXXFLAGS to the correct spot fixed it. I was not aware that the flags should be used for compiling instead of linking. New makefile:

CXX = g++
CXXFLAGS = -g -Wall
PROG  = debugging_tests
OBJS = main.o

all: $(PROG)

%.o: %.cpp
	$(CXX) -c -o $@ $< $(CXXFLAGS)

$(PROG): $(OBJS)
	$(CXX) -o $(PROG) $^ 

clean:
	rm -f *.o $(PROG)

And the proof that it works:

Sun Feb 11 jbs@dmb-gaming-laptop -> make clean
rm -f *.o debugging_tests
Sun Feb 11 jbs@dmb-gaming-laptop -> ls
main.cpp  Makefile
Sun Feb 11 jbs@dmb-gaming-laptop -> make
g++ -c -o main.o main.cpp -g -Wall
g++ -o debugging_tests main.o 
Sun Feb 11 jbs@dmb-gaming-laptop -> gdb debugging_tests 
GNU gdb (GDB) 8.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from debugging_tests...done.
(gdb) break main.cpp:5
Breakpoint 1 at 0x822: file main.cpp, line 5.
(gdb) 

Solved. big_smile

EDIT: Thanks Trilby I'll keep that in mind. smile

Last edited by JohnBobSmith (2018-02-11 15:43:15)


I have been diagnosed with Bipolar Disorder. As it turns out what I thought was once my greatest enemy is now my greatest strength.

Offline

Board footer

Powered by FluxBB