首页 文章

SDL2未定义的函数引用

提问于
浏览
3

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

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

来源/ 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 -lmingw32 -lSDL2main -lSDL2 -mwindows)

  • I 'm trying to use the ' i686-w64-mingw32'使用32位MinGW时的版本

  • 所有指定的路径似乎都是正确的

有什么想法发生了什么?

2 回答

  • 0

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

    我之前(在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)
    
  • 13

    我改变了输入参数的顺序并且它工作了,在 SLD2 之前放了 SDL2main .

相关问题