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:
-
Open the package and extract its contents (
SDL.frameworkanddevel-lite). -
Create a new project (a “Command Line Tool”), making sure to disable Automatic Reference Counting.
-
Drag the SDL framework that you extracted in step 2 (
SDL.framework) into the project. -
Drag
SDLMain.handSDLMain.minto the project from thedevel-litefolder that you extracted in step 2. -
Replace
#include "SDL.h"with#include <SDL/SDL.h>inSDLMain.m. -
Add the following
#include’s tomain.cpp.#include <SDL/SDL.h> #include "SDLMain.h" -
Make
main.cppan Objective-C++ file. One way to do this is to selectmain.cppin 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 renamemain.cpptomain.mm. -
Change the
mainfunction signature to accept achar * argv[]instead of aconst char * argv[]. -
Make sure that
SDLMain.mis compiled with the project. Select the project file in the Navigator, open the Build Phases tab, and addSDLMain.mto the list of files under Compile Sources. -
Make sure that the binary is being linked with the SDL framework.
SDL.frameworkshould be listed under Link Binary With Libraries. -
Link the Cocoa and Foundation frameworks with the project’s binary. Simply add
Cocoa.frameworkandFoundation.frameworkto the list of frameworks under Link Binary With Libraries. -
Build the program. This should finish successfully with a few warnings but no errors.
-
Build and run the program. A blank window should appear.
I hope that you find this at least as helpful as I did!