SDL错误架构x86_64"_SDL_main"的未定义符号

Wil*_*ver 11 c++ macos xcode cocoa sdl

我在我的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
Run Code Online (Sandbox Code Playgroud)

当我运行以下代码时

#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;
}
Run Code Online (Sandbox Code Playgroud)

我不知道出了什么问题,虽然看起来当我进入SDLMain.m文件并注释掉这行代码

status = SDL_main (gArgc, gArgv);
Run Code Online (Sandbox Code Playgroud)

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

csl*_*csl 19

我打赌你的main功能签名是不正确的.你用:

int main(int argc, const char * argv[])
                   ^^^^^
Run Code Online (Sandbox Code Playgroud)

但是SDL_main.h想要

int main(int argc, char *argv[])
Run Code Online (Sandbox Code Playgroud)

为什么?

你知道,SDL在编译时做了一些非常可怕的事情:它将你的main函数重命名为SDL_main,注入它自己的主函数,反过来它会调用你的函数.

请注意,如果这不起作用,那么您可能正在编译错误的标志.确保通过键入以下内容获取标志:

$ sdl-config --cflags --libs
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅简单地包括SDL标头导致链接器错误