SDL Error Undefined symbols for architecture x86_64 “_SDL_main”

瘦欲@ 提交于 2019-12-04 18:03:50

问题


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?


回答1:


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




回答2:


I had the same problem, even though my main signature was correct. I Found the answer here: SDL in XCode 4.3.2 SDLMain.o undefined symbols

Turned out the problem was that I added SDLMain.m and SDLMain.h (according to a suggestion on the libSDL website about OS X frameworks) to an existing SDL project which messed up with main. Bottom line is that you don't need those files just because you are using Cocoa -- SDL's own test apps don't use it either and run just fine on OS X.



来源:https://stackoverflow.com/questions/14543150/sdl-error-undefined-symbols-for-architecture-x86-64-sdl-main

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!