C语言 使用 SDL 时对 WinMain@16 的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18115153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined reference to WinMain@16 when using SDL
提问by Velovix
I've been having a lot of trouble getting everything working so that I can start developing on Windows, as apposed to Linux, which is what I normally use when coding. I'm having a rather bizarre issue when trying to compile an SDL program. As soon as I include the SDL library, the program refuses to compile, giving me this error:
我在让一切正常工作时遇到了很多麻烦,以便我可以在 Windows 上开始开发,就像在 Linux 上一样,这是我在编码时通常使用的。我在尝试编译 SDL 程序时遇到了一个相当奇怪的问题。一旦我包含 SDL 库,程序就会拒绝编译,给我这个错误:
c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../libmingw32.a<main.o>: In function 'main':
C:\MinGW\msys.0\src\mingwrt/../mingw/main.c:73: undefined reference to 'WinMain@16'
collect2: ld returned 1 exist status
I am using MinGW on console.
我在控制台上使用 MinGW。
To give an example, using
举个例子,使用
gcc -o test main.c
This compiles fine:
这编译得很好:
#include <stdio.h>
#include <stdlib.h>
int main(int argv, char **argc)
{
printf("Hello, world!\n");
return 0;
}
But as soon as I add #include (even without any SDL functions being called) I get the error mentioned above
但是一旦我添加 #include (即使没有调用任何 SDL 函数),我就会收到上面提到的错误
Using:
使用:
gcc -o test main.c -lSDL
This fails to compile:
这无法编译:
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
int main(int argv, char **argc)
{
printf("Hello, world!\n");
return 0;
}
Any help would be greatly appreciated! I read that this was a common issue for people who forget to have a main function, but obviously that's not my issue. I also heard that WinMain is the main function used when dealing with Windows graphical programs, but that's never been an issue for me in the past when I used to develop in Windows more.
任何帮助将不胜感激!我读到这是忘记拥有主要功能的人的常见问题,但显然这不是我的问题。我也听说 WinMain 是处理 Windows 图形程序时使用的主要函数,但过去我在 Windows 中开发更多时,这对我来说从来都不是问题。
回答by WhoBuntu
I did a little bit of searching for some more information on this error, and I found this pagewhich includes the following informaion:
我搜索了一些有关此错误的更多信息,发现此页面包含以下信息:
The only trick in getting this to compile now is to add the include path (eg: -I../SDL/include), the linker path (eg: -L../SDL/lib), and then finally adding the libraries themselves in the right order. Use:
现在编译的唯一技巧是添加包含路径(例如:-I../SDL/include)、链接器路径(例如:-L../SDL/lib),然后最后添加库自己按正确的顺序。用:
-lmingw32 -lSDLmain -lSDL
Also, don't forget to add the -mwindows flag, if your IDE doesn't add it automatically (in addition to whatever other libraries you want to link). If you don't put them in the right order, you'll get a linker error complaining about the missing symbol WinMain@16.
另外,不要忘记添加 -mwindows 标志,如果您的 IDE 没有自动添加它(除了您想要链接的任何其他库)。如果您没有按正确的顺序放置它们,您将收到一个链接器错误,抱怨缺少符号 WinMain@16。
Try recompiling with those flags above and see whether that makes a difference.
尝试使用上面的那些标志重新编译,看看是否有所不同。

