SDL2未定义的函数引用

SDL2 Undefined references to functions

本文关键字:引用 函数 未定义 SDL2      更新时间:2023-10-16

我正在尝试建立一个基于MinGW并使用SDL的C++项目。当我试图编译我的程序时,g++对我使用的每个SDL函数都说undefined reference to 'SDL_Function'

Lib/SDL2:SDL2-devel-2.0.0-mingw.tar.gz的内容来自SDL的网站

来源/Main.cpp:

#include "SDL.h"
int main(int argc, char *argv[]) {
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window *window = SDL_CreateWindow(
        "Hello World",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        640, 480, 0
    );
    SDL_Delay(3000);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

我使用Rake来稍微简化构建过程,它产生的命令是:

g++ -Wall -ILib/SDL2/i686-w64-mingw32/include/SDL2 -Dmain=SDL_main -LLib/SDL2/i686-w64-mingw32/lib -lmingw32 -lSDL2main -lSDL2 -mwindows Source/Main.cpp -o Build/sdltest.exe

下面是g++所说的:

Main.cpp:(.text+0xe): undefined reference to `SDL_Init'`
Main.cpp:(.text+0x42): undefined reference to `SDL_CreateWindow'`
Main.cpp:(.text+0x51): undefined reference to `SDL_Delay'`
Main.cpp:(.text+0x5c): undefined reference to `SDL_DestroyWindow'`
Main.cpp:(.text+0x61): undefined reference to `SDL_Quit'`
ld.exe: bad reloc address 0x20 in section `.eh_frame'`
ld.exe: final link failed: Invalid operation`
collect2.exe: error: ld returned 1 exit status`

这看起来像是一个常见的初学者问题,但我认为我通过了所有的故障排除要点:

  • 主功能具有int argc, char *argv[]
  • 我使用sdl-config中的所有标志(-Dmain=sdl_main-lmingh32-lSDL2main-lSDL2-mwindows)
  • 当使用32位MinGW时,我正在尝试使用"i686-w64-mingw32"版本
  • 所有指定的路径似乎都是正确的

知道发生了什么事吗?

尝试更改输入参数的顺序:

我以前(在Linux上)偶然发现过这个:

此调用产生错误:

g++ $(pkg-config --cflags --libs sdl2) sdl2test.cpp 
sdl2test.cpp:(.text+0x11): undefined reference to `SDL_Init'
sdl2test.cpp:(.text+0x20): undefined reference to `SDL_GetError'
sdl2test.cpp:(.text+0x34): undefined reference to `SDL_Quit'

这项工作:

g++ sdl2test.cpp $(pkg-config --cflags --libs sdl2)

我更改了输入参数的顺序,并将SDL2main放在SLD2之前。