C++: Stepping through/debugging

Discussion in 'Mac Programming' started by James-B, Oct 12, 2009.

  1. macrumors newbie

    Joined:
    Oct 6, 2009
    #1
    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?
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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.

    -Lee
     
  3. thread starter macrumors newbie

    Joined:
    Oct 6, 2009
    #3
    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. Is there something else I need to do, or am I just missing the obvious?
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    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:
    http://www.cs.cmu.edu/~gilpin/tutorial/

    -Lee
     
  5. thread starter macrumors newbie

    Joined:
    Oct 6, 2009
    #5
    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?
     

Share This Page