Monday, January 11, 2010

Debugging non-xcode code in xcode

We have some C++ code that we build using makefile system. We even use this on OS X where we could build using XCode instead, but we prefer a uniformity of build methods.

Now when a nasty and hard to trace bug crops up on an OS X build box we could try to track down the bug using good old GDB. However while I feel everyone should be able to use GDB I get sick of constantly making GDB give me what info I want. XCodes built in debugger does this nicely - in fact its just a front end to GDB. The question is how do we get our non-xcode binary to be debugged under XCode.

Turns out its EASY. (Well 90% easy).

  1. Create an empty project. ( XCode > File > New Project... )
  2. Add your binary as the default executable. (XCode > Project > New Custom Executable... )

Now you should be able to run your app through the debugger. But how do you set breakpoints? You can add your source files to the project by dragging them from finder into the XCode project sidebar. Then double click the file to open it in the XCode editor, and click in the left side by the line numbers to set breakpoints.

This worked for me most of the time. However I still had some issues setting certain breakpoints. On clicking in certain files I would get the error message
"Warning - No location found for "foo.cpp:12"
The reason for this was the file was being built from a subdirectory - its real name was autogen/foo.cpp.
So to get around this we can set the breakpoint from the gdb console. (XCode > Run > Console )
Typing "break foo.cpp:12" into the console works, presumably due to the lack of quote marks around the file/line pair.


  1. As an update to this post, since it comes up pretty high in the search results, you can do something similar to this in XCode 5.

    1. Create an empty project.
    2. Product -> Scheme -> New Scheme
    3. Select "Run" in the left menu, select the executable drop-down box, and select "Other". Add your executable there.

    1. Thanks for the update voidptr. I'm guessing you still need to manually add the source files you wish to set break-points in?