C++ SDL 2 对“WinMain@16”和几个 SDL 函数的未定义引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17048072/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:53:03  来源:igfitidea点击:

SDL 2 Undefined Reference to "WinMain@16" and several SDL functions

c++sdl

提问by Mihai

I've just installed SDL 2 and I have some serious problems. This is my code:

我刚刚安装了 SDL 2,但遇到了一些严重的问题。这是我的代码:

#include <SDL2\SDL.h>

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

I am unable to compile because I get the error described in the title:

我无法编译,因为我收到标题中描述的错误:

obj\Debug\main.o||In function SDL_main':|  
C:\Users\myuser\Desktop\test 2000\main.cpp|5|undefined reference to SDL_Init'|
C:\Users\myuser\Desktop\test 2000\main.cpp|7|undefined reference to SDL_Quit'|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32.4.1\..\..\..\libmingw32.a(main.o):main.c|| undefined reference to WinMain@16'|  
||=== Build finished: 3 errors, 0 warnings ===|

回答by k1988

I think you want

我想你想要

#define SDL_MAIN_HANDLED

in your main file, BEFORE the line

在您的主文件中,在该行之前

#include <SDL2/SDL.h>

from: https://stackoverflow.com/a/32343111/5214543

来自:https: //stackoverflow.com/a/32343111/5214543

回答by adderly

post the compiler commands. ex: g++/gcc ....

发布编译器命令。例如:g++/gcc ....

you are probably not linking the library.

您可能没有链接库。

http://content.gpwiki.org/index.php/SDL:Tutorials:Setupyou should have the path to the lib, included in the ide. (I see you are using codeblocks)

http://content.gpwiki.org/index.php/SDL:Tutorials:Setup你应该有包含在 ide 中的 lib 路径。(我看到您正在使用代码块)

add to the linker settings: -lmingw32 -lSDLmain -lSDL

添加到链接器设置:-lmingw32 -lSDLmain -lSDL

http://www.sdltutorials.com/sdl-tutorial-basics

http://www.sdltutorials.com/sdl-tutorial-basics

回答by Yash Gupta

Although the accepted answer works (if you follow it exactly, or read the comments), there's one caveat: You have to follow the order of linking libraries as given

尽管接受的答案有效(如果您完全遵循它或阅读评论),但有一个警告:您必须按照给定的链接库的顺序进行操作

g++ 01_hello_SDL.cpp -I{add correct path here}/include/SDL2 -L{add correct path here}/lib -lmingw32 -lSDL2main -lSDL2

g++ 01_hello_SDL.cpp -I{add correct path here}/include/SDL2 -L{add correct path here}/lib -lmingw32 -lSDL2main -lSDL2

And if you dont want a console to run alongside the window, add -mwindowsto the options

如果您不希望控制台与窗口一起运行,请添加-mwindows到选项

回答by barribow

You need to add these to linker libraries with the same order:

您需要以相同的顺序将这些添加到链接器库中:

mingw32
SDL2main
SDL2

Note that the order is important.

请注意,顺序很重要。

回答by Leroy Dunn

I struggled with this forever.

我一直为此而挣扎。

Simply move the sdl files out of your program files to your desktop or documents etc and link them to C from there.

只需将 sdl 文件从程序文件中移到桌面或文档等,然后从那里将它们链接到 C。

Think it has something to do with Windows not allowing C to access them or something.

认为这与 Windows 不允许 C 访问它们或其他东西有关。

Hope this helps Cool

希望这有助于酷

回答by Eyfenna

I solved this by exchanging the

我通过交换解决了这个问题

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

line with

符合

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow )

No idea why to be honest, then again I moved on to the SFML.

老实说,不知道为什么,然后我又转向了 SFML。

回答by Charles Kirk

I just had a similar issue when trying to build and run a project in Eclipse CDT 1909 on Windows 10 that I had originally created in Eclipse on MacOS.

在尝试在 Windows 10 上的 Eclipse CDT 1909 中构建和运行我最初在 MacOS 上的 Eclipse 中创建的项目时,我遇到了类似的问题。

I found the following steps worked using the minGW64 Compiler.

我发现使用 minGW64 编译器执行以下步骤。

RMB Click on the project and choose properties. In Project Properties > C/C++ Build create a new C/C++ Build Configuration by LMB Clicking Manage Configurations and LMB Clicking New, name it Debug_Windows, set Copy Setting from to Default configuration, make it active, LMB Click OK, set the Configuration to Debug_Windows.

RMB 单击项目并选择属性。在 Project Properties > C/C++ Build 中,通过 LMB 单击 Manage Configurations 和 LMB 单击 New 创建一个新的 C/C++ Build Configuration,将其命名为 Debug_Windows,将 Copy Setting from 设置为 Default configuration,使其处于活动状态,LMB 单击 OK,将 Configuration 设置为调试_Windows。

Set the Project Properties > C/C++ Build > Builder Settings > Build Command > Current Builder to CDT Internal Builder

将项目属性 > C/C++ Build > Builder Settings > Build Command > Current Builder 设置为 CDT Internal Builder

Set the Project Properties > C/C++ Build > Builder Settings > Tool Chain Editor > Current toolchain to MinGW GCC

将 Project Properties > C/C++ Build > Builder Settings > Tool Chain Editor > Current toolchain 设置为 MinGW GCC

Set the Project Properties > C/C++ Build > Builder Settings > Tool Chain Editor > Current builder to CDT Internal Builder

将 Project Properties > C/C++ Build > Builder Settings > Tool Chain Editor > Current builder 设置为 CDT Internal Builder

Add the following path to the Project Properties > C/C++ Build > Settings > GCC C++ Compiler > Includes "C:\Applications\Library\SDL2-2.0.9\x86_64-w64-mingw32\include\SDL2"

将以下路径添加到 Project Properties > C/C++ Build > Settings > GCC C++ Compiler > Includes "C:\Applications\Library\SDL2-2.0.9\x86_64-w64-mingw32\include\SDL2"

Add the following path to Project Properties > C/C++ Build > Settings > GCC C++ Linker > Libraries "C:\Applications\Library\SDL2-2.0.9\x86_64-w64-mingw32\lib"

将以下路径添加到 Project Properties > C/C++ Build > Settings > GCC C++ Linker > Libraries "C:\Applications\Library\SDL2-2.0.9\x86_64-w64-mingw32\lib"

add the following libraries to Project Properties > C/C++ Build > Settings > GCC C++ Linker > Libraries, in the order SLD2main SDL

将以下库添加到 Project Properties > C/C++ Build > Settings > GCC C++ Linker > Libraries,按照 SLD2main SDL 的顺序

Navigate to Project Properties > Run/Debug Settings, LMB Click New, select C/C++ Application, LMB Click OK, in the C/C++ Application field enter Debug_Windows\project_name.exe (replacing project_name with the correct value).

导航到 Project Properties > Run/Debug Settings,LMB 单击 New,选择 C/C++ Application,LMB 单击 OK,在 C/C++ Application 字段中输入 Debug_Windows\project_name.exe(用正确的值替换 project_name)。

Change the Build Configuration to Debug_Windows.

将构建配置更改为 Debug_Windows。

Close the project properties.

关闭项目属性。

Add the preprocessor directive #define SDL_MAIN_HANDLED at the top of the file containing the main function. Without the preprocessor directive the program will not link correctly.

在包含主函数的文件顶部添加预处理器指令 #define SDL_MAIN_HANDLED。如果没有预处理器指令,程序将无法正确链接。

To run the program RMB Click on the project and choose Run as > Run Configurations and choose the run configuration created for the build.

运行程序 RMB 单击项目并选择 Run as > Run Configurations 并选择为构建创建的运行配置。

Copy SDL2.dll to the top level of the project directory structure, as if it is copied to the Debug or Debug_Windows directory it is deleted every time the project is built.

将 SDL2.dll 复制到项目目录结构的顶层,就像复制到 Debug 或 Debug_Windows 目录一样,每次构建项目时都会将其删除。

The key points that I found were that it is not necessary to include mingw32 in the Linker Libraries and #define SDL_MAIN_HANDLED needs to be in the file containing main and before #include .

我发现的关键点是没有必要在链接器库中包含 mingw32 并且 #define SDL_MAIN_HANDLED 需要在包含 main 的文件中并且在 #include 之前。