如何在 Visual Studio 2008 Express 中编译和链接最小的 SDL 程序?
我正在尝试通过 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,我都会得到此错误。 将 main
定义为 main()
或 SDL_main()
会产生相同的错误,无论是否有 extern "C"
。
编辑:我通过不在 main.cpp 中包含 SDL.h 解决了这个问题 - 这是我独立于问题进行的重构。 类似的解决方案是在定义函数之前使用#undef main
。
I'm trying to use SDL in C++ with Visual Studio 2008 Express. The following program compiles but does not link:
#include <SDL.h>
int main(int argc, char *argv[])
{
return 0;
}
The link error is:
LINK : fatal error LNK1561: entry point must be defined
I get this regardless of how or if I link with SDL.lib and SDLmain.lib. Defining main
as main()
or SDL_main()
gives the same error, with or without extern "C"
.
Edit: I solved this by not including SDL.h in main.cpp - a refactoring I did independent of the problem. A similar solution would be to #undef main
right before defining the function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我目前没有可用的 VC++,但我已经多次看到这个问题。
您需要创建一个 Win32 项目而不是控制台项目。 Win32 项目需要 WinMain 函数作为程序入口点。 SDLmain.lib 包含此入口点,并且 SDL_main.h 头文件具有将 main 函数重新映射到 SDL_main 的宏。 该函数由 SDLmain 库中的入口点调用。
main 函数必须具有以下签名:
还需要在 main 函数的声明之前包含 SDL.h,并且需要链接到 SDL.lib 和 SDLmain.lib。
看起来你正在做这个。 所以,我的猜测是您有一个控制台项目设置。因此,链接器正在寻找要调用的主函数,但它被宏 SDL_main.h 重新映射到 SDL_main< /强>。 因此,链接器找不到入口点并放弃!
I don't have VC++ available at the moment, but I have seen this issue several times.
You need to create a Win32 project as opposed to a console project. A Win32 project expects a WinMain function as a program entry point. SDLmain.lib contains this entry point and the SDL_main.h header file has a macro that remaps your main function to SDL_main. This function is called by the entry point in the SDLmain library.
The main function must have the following signature:
It is also required to include SDL.h before the declaration of your main function, and you need to link to both SDL.lib and 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!
对我来说,在 main() 之前添加以下几行很有帮助:
德语维基百科还建议添加这些行:
虽然当我尝试第二个解决方案时仍然有链接错误。
To me it helped to add the following lines before main():
German Wikipedia also suggests to add these lines instead:
Though I still had link errors when I tried second solution.
链接器找不到入口点。 这意味着您的 main() 函数未被识别为入口点。
如果您有 .def 文件,请将其删除。
另外,如果您已将项目设置为使用 unicode 而不是 mbcs 进行编译,则必须使用 wmain() 而不是 main()。
The linker can't find the entry point. Which means your main() function is not recognized as the entry point.
If you have a .def file, remove it.
Also, if you've set up your project to compile with unicode and not as mbcs, you have to use wmain() instead of main().