我正在尝试建立一个基于MinGW并使用SDL的C++项目。当我尝试编译程序时,g++对我使用的每个SDL函数说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[]
任何线索,这是怎么回事?
最佳答案
尝试更改输入参数的顺序:
之前(在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)