11

我在我的 mac os x 上使用 C++ 和 SDL Cocoa 和 Foundation 框架。我收到以下错误

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

当我运行以下代码时

#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;
}

我不知道出了什么问题,尽管似乎当我进入 SDLMain.m 文件并注释掉这行代码时

status = SDL_main (gArgc, gArgv);

程序编译没有问题。但是,它不起作用。没有窗户像它应该的那样打开。有任何想法吗?

4

2 回答 2

19

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

于 2013-02-03T01:34:22.183 回答
3

我有同样的问题,即使我的主要签名是正确的。我在这里找到了答案:SDL in XCode 4.3.2 SDLMain.o undefined symbols

原来问题是我将 SDLMain.m 和 SDLMain.h(根据 libSDL 网站上关于 OS X 框架的建议)添加到与 main 混淆的现有 SDL 项目中。底线是您不需要这些文件,因为您使用的是 Cocoa——SDL 自己的测试应用程序也不使用它,并且在 OS X 上运行得很好。

于 2016-08-20T02:17:50.390 回答