Debugging C++ code (UNIX)
This assumes you're inside a UNIX terminal window (e.g., PuTTY session to remote UNIX box).
Compile a .cpp source file in the GNU debugger:
g++ -g -Wall -o mysourcefile.out mysourcefile.cpp
Before starting gdb, make sure you're in the same directory as the executable just compiled above. Then call gdb:
add GNU
Then start the gdb debugger:
gdb mysourcefile.out
Things you can do here:
1) Set a breakpoint at a given function (e.g., main):
break main
2) Set a breakpoint at a given line (e.g., line 12):
break mysourcefile.cpp:12
3) Set a watch point at a given expression:
watch x>y
4) Find the type of that expression:
whatis x>y
5) OK, that was trivial, clearly it's boolean. More usefully, you can find its value so far (true or false in the GNU C++ compiler, 1 or 0 in others, I guess) with
print x>y
6) Step into the program (execute a certain number of lines after the debugger stopped at your breakpoint):
step <number of lines>
If you set a break point, run
will execute the code up to then, at which point you can choose next
to move on to the next line, or continue
, for the thing to run until it exits normally or an error is found.