Importing MySQL into XCode

I’m dabbling in Mac OS X (desktop, but eventually iOS) app development for a project at work. At the moment, that’s taking the form of a proof-of-concept interface to MySQL, to be an eventual GUI seeder and migration application for moving data from ScheduleThing to the new application.

Here’s the trouble: Cocoa doens’t really come with any database integration out of the box (that I can tell, anyway), and so you have to include the mysql client yourself. And, of course, Mac has trouble finding it.

So, here’s what you do. If you haven’t already, install the mysql client library via homebrew. Then, you have to tell XCode where to find it. One part of this is obvious, and the other wasn’t – at least not to me.

The obvious part: Open the Build Settings manager in XCode (click on the project in the project manager bar (left-hand menu in the standard XCode view) and then click on "Build Settings" at the top of the overview window). Edit "Header Search Paths" under "Search Paths" to include the mysql client lib headers (for me: /usr/local/Cellar/mysql/5.5.25a/include).

The not-so-obvious part You also have to tell the linker where to actually find libmysql.dylib. OK, maybe that part’s obvious. But what wasn’t obvious is that the compiler apparently can’t find it by just including the libraries path. I mean, the "obvious" thing to do would be to add /usr/local/Cellar/mysql/5.5.25a/lib to "Library Search Paths" and click to make the search recursive, but that doens’t work.

So, what you have to do instead is add this to "Other Linking Flags" under "Linking": /usr/local/Cellar/mysql/5.5.25a/lib/libmysqlclient.dylib.

And then, provided you actually imported mysql in your code (in my case, I was trying to call mysql_get_client_info(), so I had to #import <mysql.h> and #import <my_global.h>), everything should work.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>