如何在 Visual Studio 2008 Express 中编译和链接最小的 SDL 程序?

发布于 2024-07-11 01:48:57 字数 540 浏览 4 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雾里花 2024-07-18 01:48:57

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

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

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

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

还需要在 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:

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

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!

起风了 2024-07-18 01:48:57

对我来说,在 main() 之前添加以下几行很有帮助:

#ifdef _WIN32
#undef main
#endif

德语维基百科还建议添加这些行:

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

虽然当我尝试第二个解决方案时仍然有链接错误。

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.

梦情居士 2024-07-18 01:48:57

链接器找不到入口点。 这意味着您的 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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文