View Full Version : C++: Stepping through/debugging

Oct 12, 2009, 04:14 PM
Dear All, I've just started (trying to) teach myself C++. My only prior experience of programming is writing VBA code where, after I've written it, I can step through it line by line and see what's happening. Can I get X-Code to do the equivalent with C++ code?

Oct 12, 2009, 04:18 PM
Yes. XCode is integrated with the GDB debugging tool. If you double-click to the left of the line you want to start debugging from, then choose "compile and debug" instead of "compile and run", you'll be presented with the debugger when your code reaches that line. From there you can single step through code, display variable contents, etc.


Oct 12, 2009, 08:05 PM
Thanks for the reply. I can't find "compile and debug" though, or "compile and run" for that matter. I run projects via terminal. All I've done is what was suggested on this thread (http://forums.macrumors.com/showthread.php?t=798792). Is there something else I need to do, or am I just missing the obvious?

Oct 12, 2009, 09:18 PM
If you're compiling and running from the terminal(which is A-OK), you're not using XCode. To get debugging symbols in your compiled binary, just add a "-g" switch in your g++ command line when compiling. Now your binary is "ready to debug". You can then run:
gdb ./mybinary
gdb> b main
gdb> run

Then to single step:
gdb> s

To go to the next line (stepping over functions):
gdb> n

There is a tutorial for using the debugger here:


Oct 13, 2009, 04:30 AM
I see. Thanks a lot. This seems to be working. Is there any particular advantage to using X-code to debug, or is it just as good doing it as you've suggested?