XCode Primer

Hi all. Got an annoying newbie question for you. So, I've been developing at the command line all my life with gcc and make, but my company just bought me a PowerBook replete with XCode.  So, I figured I'd give it a fair shake, but from opening the application and playing around for a few hours, it's still not obvious to me how to build my old linux source code in it... even though I know it's really just using gcc and doing the same things I normally would with make (I think).   For example, I've got one application written in c that has a main program source file, a header with structs and fucntions used in the main program, a source file with the header's functions in it, and it needs to link to a shared library (which, I gather, are dylib's in OSX):



Needs to link to: libmysqlclient.dylib in /usr/lib/

So, I was hoping someone would be kind enough to walk me through setting up a project that contains these files in the correct arrangement such that when I hit the build button, it builds.  (I.e. what kind of project to I open, how do I answer the dialogues, etc.)

Thanks so much!
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

brettmjohnsonConnect With a Mentor Commented:
It looks like its failing to load the shared library, and hanging trying to report
an undefined symbol error.  This looks like a loader problem or possibly a
problem with the dynamic lib.  Were you supposed to link against some other
library as well?  And yes XCode is free. The latest version is available at:


I am assuming your program is a traditional command-line tool
ported from Linux and (at this point) not using Apple-specific toolkits
(like AppKit, CoreFoundation, etc).

- Launch /Developer/Applications/Xcode.app
- Select "File->New Project" from the menu
- Select "Command Line Application - Standard Tool" for the Project Type
- Name the project accordingly "matchd", Put it in the same directory as your source files
- New Project annoyingly creates "main.c" for you, so you might as well delete it now from Project-Source
- Select "Project->Add to Project" from the menu to add the source files
  - Select the source files to add to the project
  - Use CMD-click to select multiple files (CMD is the Apple key next to spacebar)
  - Click the "Add" button after selecting the three source files
  - Specify "Reference Type: Relative to Project"
- Select "Project->Add to Project" from the menu to add the library
  - Type '/' to bring up the hidden "Go To Folder" dialog box (BAD UI. BAD, BAD, UI!!)
  - Complete typing "/usr/lib" in the Go To Folder dialog and Click "GoTo" button
  - Select libmysqlclient.dylib and click the "Add" button
  - Specify "Reference Type: Absolute Path"
- Click the "Build" button and iterate, fixing up build errors.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

spike3382Author Commented:
Thanks, Brett.  I'd been wondering about how to get to /usr/lib through the gui.  That is an interesting decision on apple's part.  And yes, it's just a command line tool.

However, I followed your instructions (with a few interpolations... I don't have CommandLine Application - Standard Tool; but I do have Tool - Standard Tool, so I used that.  I also don't have Project->Add to Project but I do have Project->Add File, so I took an educated guess ;-) ), and everything seemed okay.  It compiled with no errors.  However, if I hit Build and Run, it opens a fresh terminal window that stays blank and then the CPU usage goes to 100% and nothing happens.  I modified the program specifically so that it would return artificially quickly with a statement written to the terminal, and still all it does is descend into some infinite loop that I seem to have no control over and takes up 100% CPU forever...  I can compile it with make in the project directory and everything goes swimingly.

Any thoughts on what I'm doing wrong?  Thanks!

(thanks for the references too, by the way).

The minor menu differences are probably due to differences in XCode version (I'm at v1.5).

If the program doesn't work ... time to debug it.  On the XCode project menu click and hold
the "Build and Go" icon until a drop-down list appears, then select "Build and Debug".
This will build the debuggable version of the product and pop you into the GUI front end
to gdb.   You can click the "Breakpoints" icon on the Debugger toolbar and add a breakpoint
for main().

spike3382Author Commented:
Yeah, I'm using XCode 1.0, apparently.

I put a breakpoint next to "int main(.." and a few other places, and when I hit build and debug it never got to any of them, which makes me think that perhaps it's not my problem but XCode's?  Do you think this is a bug from using XCode 1.0?  I'm unfamiliar with Apple's upgrade policy... I assume it's free....  

Anyway, if I pause the program durring debug execution, all I get is the list of the contents of the thread on the left.  None of them cause any more information to come up in the right or bottom panes.  It looks like this:

#0      0x8fe26998 in __dyld_thread_self_trap
#1      0x8fe161ac in __dyld_linkedit_error_enter
#2      0x8fe0a360 in __dyld_check_and_report_undefineds
#3      0x8fe11690 in __dyld_link_in_need_modules
#4      0x8fe1124c in __dyld_bind_lazy_symbol_reference
#5      0x8fe01340 in __dyld_stub_binding_helper_interface
#6      0x7fdf0ce8 in findSymbolInSet
#7      0x7fdf0d6c in undefinedHandler
#8      0x8fe0a398 in __dyld_check_and_report_undefineds
#9      0x8fe11690 in __dyld_link_in_need_modules
#10      0x8fe11464 in __dyld_bind_symbol_by_name
#11      0x8fe148b0 in __dyld__dyld_NSLookupAndBindSymbol
#12      0x90045974 in NSLookupAndBindSymbol
#13      0x00001d54 in start
#14      0x00001c70 in start
#15      0x00001ae4 in start

So, I'm not sure if anyone has any intuition on why this isn't working... as I said before this is totally fine code and compiles and runs beautifully from the command line, I don't understand what extra work XCode is doing that's causing it not to be able to run.  If anyone has any intuition on this, I'd be forever grateful.

Thanks for your time!

spike3382Author Commented:
Well, it's working now, and I figured I'd post this incase anyone was having a similar problem.  I upgraded to XCode Tools 1.5, and followed the above steps verbatim and the program compiles and runs great.  So, who knows, I guess XCode v1.0 might have an issue.

Thanks a lot, Brett!  I learned a lot.

All Courses

From novice to tech pro — start learning today.