C++ 无法使用 MinGW 链接到 SDL2 函数

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

Unable to link to SDL2 functions using MinGW

c++mingwsdlcodeblocks

提问by user3052572

I'm relatively new to programming and I've decided to give SDL a try, but I'm a bit stuck. I haven't been able to build the project in codeblocks and I get 'undefined reference' to all SDL functions. I've seen a lot of similar questions here, but none of the solutions seems to help. I've already added the \include\SDL2 and the \lib folders to search directories, I've added SDL2Main and SDL2 to link libraries in linker options, I've even added -mwindows to other linker options. Also, I tried linking against the 64-bit version as well, but things got even worse.

我对编程比较陌生,我决定尝试一下 SDL,但我有点卡住了。我无法在代码块中构建项目,并且我获得了对所有 SDL 函数的“未定义引用”。我在这里看到了很多类似的问题,但似乎没有一个解决方案有帮助。我已经添加了 \include\SDL2 和 \lib 文件夹来搜索目录,我已经添加了 SDL2Main 和 SDL2 来链接链接器选项中的库,我什至将 -mwindows 添加到其他链接器选项。此外,我也尝试链接到 64 位版本,但情况变得更糟。

Here's my source code, pretty much copied straight out of the tutorial I started:

这是我的源代码,几乎直接从我开始的教程中复制而来:

#include <SDL.h>
SDL_Window* g_pWindow = 0;
SDL_Renderer* g_pRenderer = 0;
int main(int argc, char* args[])
    {
        // initialize SDL
    if(SDL_Init(SDL_INIT_EVERYTHING) >= 0)
    {
    // if succeeded create our window
        g_pWindow = SDL_CreateWindow("Chapter 1: Setting up SDL",
        SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
        640, 480,
        SDL_WINDOW_SHOWN);
        // if the window creation succeeded create our renderer
        if(g_pWindow != 0)
        {
            g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
        }
    }
    else
    {
        return 1; // sdl could not initialize
    }
    // everything succeeded lets draw the window
    // set to black // This function expects Red, Green, Blue and
    // Alpha as color values
    SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, 255);
    // clear the window to black
    SDL_RenderClear(g_pRenderer);
    // show the window
    SDL_RenderPresent(g_pRenderer);
    // set a delay before quitting
    SDL_Delay(5000);
    // clean up SDL
    SDL_Quit();
        return 0;
}

And here's the build log:

这是构建日志:

mingw32-g++.exe -LC:\dev\sdl\SDL2-2.0.1\x86_64-w64-mingw32\lib  -o bin\Debug\GeometryProject.exe obj\Debug\main.o   -mwindows  C:\MinGW\lib\libmingw32.a C:\dev\sdl\SDL2-2.0.1\x86_64-w64-mingw32\lib\libSDL2main.a C:\dev\sdl\SDL2-2.0.1\x86_64-w64-mingw32\lib\libSDL2.a 
obj\Debug\main.o: In function `SDL_main':
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:7: undefined reference to `SDL_Init'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:13: undefined reference to `SDL_CreateWindow'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:17: undefined reference to `SDL_CreateRenderer'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:27: undefined reference to `SDL_SetRenderDrawColor'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:29: undefined reference to `SDL_RenderClear'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:31: undefined reference to `SDL_RenderPresent'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:33: undefined reference to `SDL_Delay'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:35: undefined reference to `SDL_Quit'
C:\MinGW\lib\libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
9 errors, 0 warnings (0 minutes, 0 seconds)

Is there anything else I could try? I really would like to get this running and would appreciate any help.

还有什么我可以尝试的吗?我真的很想让它运行,并希望得到任何帮助。

回答by Xonar

A possible problem is that your linking to a SDL2 library for a different architecture.

一个可能的问题是您链接到不同架构的 SDL2 库。

You should be using

你应该使用

SDL2-2.0.1\i686-w64-mingw32

instead of

代替

SDL2-2.0.1\x86_64-w64-mingw32

Also after setting the library search path use this notation to link to libraries

同样在设置库搜索路径后,使用此符号链接到库

-lmingw32 -lSDL2main -lSDL2

It's much easier to read.

阅读起来要容易得多。

回答by ollo

Instead of adding full path, try -LC:/PATH_TO_SDL -lSDL2main -lSDL2(or only one SDL option) instead; the liband .ais already known by the linker.

与其添加完整路径,不如尝试-LC:/PATH_TO_SDL -lSDL2main -lSDL2(或仅使用一个 SDL 选项);在lib.a已经被链接器闻名。

(not sure if you have to use either /or \)

(不确定您是否必须使用/\

Since you are using C++, replace your include with this:

由于您使用的是 C++,请将您的包含替换为:

extern "C"
{
    #include "SDL.h"
}

This tells the compiler to treat SDL as C - and not C++ - code.

这告诉编译器将 SDL 视为 C - 而不是 C++ - 代码。

See also: SDL2 won't link properly

另请参阅:SDL2 无法正确链接