7

我正在尝试通过 Visual Studio 2008 Express 在 C++ 中使用 SDL。以下程序编译但不链接:

#include <SDL.h>

int main(int argc, char *argv[])
{
    return 0;
}

链接错误是:

LINK : fatal error LNK1561: entry point must be defined

无论我如何或是否与 SDL.lib 和 SDLmain.lib 链接,我都会得到这个。定义mainmain()SDL_main()给出相同的错误,有或没有extern "C".

编辑:我通过在 main.cpp 中不包含 SDL.h 解决了这个问题——我做了一个独立于问题的重构。一个类似的解决方案是#undef main在定义函数之前。

4

3 回答 3

6

我目前没有可用的 VC++,但我已经多次看到这个问题。

您需要创建一个 Win32 项目而不是控制台项目。Win32 项目需要一个WinMain函数作为程序入口点。SDLmain.lib 包含此入口点,并且 SDL_main.h 头文件有一个宏,可将您的 main 函数重新映射到 SDL_main。此函数由 SDLmain 库中的入口点调用。

主函数必须具有以下签名:

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

还需要在声明主函数之前包含 SDL.h,并且需要链接到 SDL.lib 和 SDLmain.lib。

It looks like you are doing this. So, my guess is that you have a console project setup. Therefore, the linker is looking for a main function to call, but it is getting remapped to SDL_main by the macro SDL_main.h. So, the linker can't find an entry point and gives up!

于 2008-12-28T15:53:55.617 回答
3

To me it helped to add the following lines before main():

#ifdef _WIN32
#undef main
#endif

German Wikipedia also suggests to add these lines instead:

#ifdef _WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif

Though I still had link errors when I tried second solution.

于 2009-03-20T17:19:41.930 回答
0

链接器找不到入口点。这意味着您的 main() 函数不被识别为入口点。

如果您有 .def 文件,请将其删除。

此外,如果您已将项目设置为使用 unicode 而不是 mbcs 进行编译,则必须使用 wmain() 而不是 main()。

于 2008-12-28T15:31:49.780 回答