Saturday, December 24, 2011

Setting up SDL to work with Xcode 4.2 on Mac OS X 10.7

Until recently I have been using os x 10.6 and xcode 3.2. When I updated my os to 10.7 and xcode to 4.2, I had no idea how to set up SDL because xcode 4+ is so different from xcode 3.2 and previous versions. Here is how I have done it and so far it has worked great.

Step 1. Make sure you are using lion and xcode 4.2 or else this is not the right tutorial for you.

Step 2. Download SDL and any SDL extensions you want. Here are the links that are valid as of 12/24/2011

Download the runtime library for Mac OS X.

Download the binary

Download the binary

Download the binary

Step 3. Mount all of the dmg files you just downloaded (double click them) and put the framework folder that would say something like "SDL.framework" or "SDL_image.framework" into /Library/Frameworks/. This allows xcode to see the framework in the following steps. If you don't want to or don't have permission, put it anywhere you want on your computer.

Step 4. Make a new command line xcode project in c++ or foundation, whichever language you choose.

Step 5. Go to project settings and under Build Phases go to the Link Binary With Libraries option.
Under this option click the + button and add the following to your project:
Optional: SDL_ttf.framework, SDL_image.framework, SDL_mixer.framework

If you did not put the frameworks that you previously downloaded into /Library/Frameworks/, then click add other and add it from where ever you put it.

Step 6. Next, click the Build Settings tab right next to the Build Phases tab and go down to the category "Search paths." Double click the blank space that is occupying the header search paths and add to it the path for each of the SDL framework headers you added. For SDL.framework, mine is "/Library/Frameworks/SDL.framework/Headers"

Step 7. Add the SDLmain.h and SDLmain.m to your project. If you are using SDL for c++ and are wondering what the SDLmain.m file is, it is an Objective-C file because SDL is written in Objective-C for the Mac.

Step 8. In your main.cpp or main.m file, copy and paste the below code in, build and run, and if you get no errors you are set.

#include "SDL.h"

int main( int argc, char* args[] )
    //Start SDL
    //Quit SDL
    return 0;    

If you are planning on using SDL_image, SDL_ttf or SDL_mixer, the includes for them look like this.

#include SDL_image.h
#include SDL_ttf.h
#include SDL_mixer.h


  1. Compiling that code gives me: Undefined symbols for architecture x86_64:
    "_main", referenced from:
    start in crt1.10.6.o
    (maybe you meant: _SDL_main)
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

  2. This tutorial has saved me hours of annoyance. For that you have my thanks.