Chris Frederick

Xcode 4 - How to Install SDL on Mac OS X 10.7/10.8 Lion

I really wanted to install and use the Simple Directmedia Layer to begin building some game prototypes in XCode, but I kept running into the same maddening compiler errors.

Undefined symbols for architecture x86_64:
  "_SDL_main", referenced from:
      -[SDLMain applicationDidFinishLaunching:] in SDLMain.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I naturally searched for a solution on Google and eventually found this video, which explains how to successfully build and run a program in XCode 4 using the SDL. I generally prefer to read instructions, though, so I'm essentially going to transcribe the major steps outlined in the video for your convenience. I hope that you find this at least as helpful as I did!

  1. Download the SDL 1.2 package from the SDL website.

  2. Open the package and extract its contents (SDL.framework and devel-lite).

  3. Launch XCode.

  4. Create a new project (a “Command Line Tool”), making sure to disable Automatic Reference Counting.

  5. Drag the SDL framework that you extracted in step 2 (SDL.framework) into the project.

  6. Remove unnecessary code from main.cpp.

  7. Drag SDLMain.h and SDLMain.m into the project from the devel-lite folder that you extracted in step 2.

  8. Replace #include "SDL.h" with #include <SDL/SDL.h> in SDLMain.m.

  9. Add the following #include's to main.cpp.

    #include <SDL/SDL.h>
    #include "SDLMain.h"
    
  10. Make main.cpp an Objective-C++ file. One way to do this is to select main.cpp in the Navigator, open the File Inspector in the Utilities on the right side of the XCode window, and then select “Objective-C++ Source” as the File Type. You could also simply rename main.cpp to main.mm.

  11. Change the main function signature to accept a char * argv[] instead of a const char * argv[].

  12. Make sure that SDLMain.m is compiled with the project. Select the project file in the Navigator, open the Build Phases tab, and add SDLMain.m to the list of files under Compile Sources.

  13. Make sure that the binary is being linked with the SDL framework. SDL.framework should be listed under Link Binary With Libraries.

  14. Link the Cocoa and Foundation frameworks with the project's binary. Simply add Cocoa.framework and Foundation.framework to the list of frameworks under Link Binary With Libraries.

  15. Build the program. This should finish successfully with a few warnings but no errors.

  16. Stub out a simple SDL implementation.

  17. Build and run the program. A blank window should appear.

  18. Set a window title with SDL_WM_SetCaption.

  19. Recompile and run the program again.