7

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

#include <SDL2\SDL.h>

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

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

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.4.1\..\..\..\libmingw32.a(main.o):main.c|| undefined reference to WinMain@16'|  
||=== Build finished: 3 errors, 0 warnings ===|
4

8 回答 8

22

我想你想要

#define SDL_MAIN_HANDLED

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

#include <SDL2/SDL.h>

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

于 2017-09-28T02:25:36.740 回答
8

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

你可能没有链接图书馆。

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

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

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

于 2013-06-11T16:42:46.177 回答
7

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

mingw32
SDL2main
SDL2

请注意,顺序很重要。

于 2018-01-18T19:13:06.160 回答
5

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

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

如果您不希望控制台在窗口旁边运行,请添加-mwindows到选项

于 2015-04-14T08:03:11.790 回答
2

我一直为此苦苦挣扎。

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

认为这与 Windows 不允许 C 访问它们有关。

希望这有助于酷

于 2013-08-31T19:04:45.650 回答
1

如果您使用的是 cLion IDE 中默认的 CMakeLists.txt,那么您应该在末尾添加这一行:

target_link_libraries(space_fighters mingw32 SDL2main SDL2)

我有同样的问题,我的情况是因为我错过了“mingw32”

于 2021-01-10T22:49:44.520 回答
0

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

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

符合

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

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

于 2016-12-26T19:18:44.823 回答
-2

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

我发现以下步骤使用 minGW64 编译器有效。

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

将 Project Properties > C/C++ Build > Builder Settings > Build Command > Current Builder 设置为 CDT Internal Builder

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

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

将以下路径添加到 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++ Linker > Libraries "C:\Applications\Library\SDL2-2.0.9\x86_64-w64-mingw32\lib"

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

导航到项目属性 > 运行/调试设置,LMB 单击新建,选择 C/C++ 应用程序,LMB 单击确定,在 C/C++ 应用程序字段中输入 Debug_Windows\project_name.exe(将 project_name 替换为正确的值)。

将构建配置更改为 Debug_Windows。

关闭项目属性。

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

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

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

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

于 2019-11-28T15:55:54.087 回答