SDL Error Undefined symbols for architecture x86_64 "_SDL_main"

William Oliver picture William Oliver · Jan 27, 2013 · Viewed 8.9k times · Source

I am using C++ with the SDL Cocoa and Foundation framework on my mac os x. I get the following error

Undefined symbols for architecture x86_64:
  "_SDL_main", referenced from:
      -[SDLMain applicationDidFinishLaunching:] in SDLMain.o
ld: symbol(s) not found for architecture x86_64

when I run the following code

#import <Foundation/Foundation.h>
#import <SDL/SDL.h>
#include "SDLMain.h"
int main(int argc, const char * argv[])
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_SetVideoMode(640,480,32,SDL_DOUBLEBUF);
    SDL_Event event;
    bool isRunning = true;
    while(SDL_PollEvent(&event)){
        if(event.type == SDL_QUIT){
            isRunning=false;
        }
    }

    SDL_Quit();
    return 0;
}

I have no idea what is wrong, although it seems that when I go into the SDLMain.m file and comment out this line of code

status = SDL_main (gArgc, gArgv);

the program compiles with no problems. However, it doesn't work. No window opens like its supposed to. Any ideas?

Answer

csl picture csl · Feb 3, 2013

I bet your main function signature is incorrect. You use:

int main(int argc, const char * argv[])
                   ^^^^^

but SDL_main.h wants

int main(int argc, char *argv[])

Why?

You see, SDL does something really horrific when compiling: It renames your main function to SDL_main, injecting its own main function which, in turn, calls yours.

Note that if this doesn't work, then you may be compiling with wrong flags. To be sure, get the flags by typing:

$ sdl-config --cflags --libs

For more information, see Simply including SDL header causes linker error