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.
EDIT: Thanks Trilby I'll keep that in mind.
]]>Note that when you built directly on the command line, you did include your CXXFLAGS content.
]]>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!
]]>