我正在尝试用SDL编译这段代码
#include <SDL.h>
int main(int argc, char * argv[]){
return 0;
}
文件本身编译好
g++ -c main.cpp -ISDL/include
但是使用g ++在cygwin中使用以下命令进行编译
g++ -o test main.o -lSDL2 -lSDL2main -L SDL/lib/x64
产生了这个巨大的错误......在我看来,这似乎是SDL本身的一个问题......
$ g++ -o test main.o -lSDL2 -lSDL2main -L SDL/lib/x64
Warning: corrupt .drectve at end of def file
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[main]+0xe): undefined reference to `SDL_SetMainReady'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[main]+0xe): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SDL_SetMainReady'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[WinMain]+0x29): undefined reference to `SDL_wcslen'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[WinMain]+0x29): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SDL_wcslen'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[WinMain]+0x46): undefined reference to `SDL_iconv_string'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[WinMain]+0x46): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SDL_iconv_string'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[WinMain]+0x7d): undefined reference to `SDL_malloc'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[WinMain]+0x7d): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SDL_malloc'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[WinMain]+0xb8): undefined reference to `SDL_free'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[WinMain]+0xb8): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SDL_free'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[WinMain]+0xc2): undefined reference to `SDL_free'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[WinMain]+0xc2): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SDL_free'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[ParseCommandLine]+0x4a): undefined reference to `SDL_isspace'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[ParseCommandLine]+0x4a): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SDL_isspace'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[ParseCommandLine]+0x146): undefined reference to `SDL_isspace'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[ParseCommandLine]+0x146): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SDL_isspace'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[ShowError]+0x10): undefined reference to `__imp___iob_func'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[ShowError]+0x10): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `__imp___iob_func'
collect2: Fehler: ld gab 1 als Ende-Status zurück
提前致谢
修改
更改库顺序会产生以下错误
$ g++ -o test main.o -lSDL2main -lSDL2 -L SDL/lib/x64
Warning: corrupt .drectve at end of def file
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[ShowError]+0x10): undefined reference to `__imp___iob_func'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[ShowError]+0x10): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `__imp___iob_func'
SDL/lib/x64/SDL2.lib(SDL2.dll.b):(.text+0x2): relocation truncated to fit: R_X86_64_32 against symbol `__imp_SDL_SetMainReady' defined in .idata$5 section in SDL/lib/x64/SDL2.lib(SDL2.dll.b)
SDL/lib/x64/SDL2.lib(SDL2.dll.b):(.text+0x2): relocation truncated to fit: R_X86_64_32 against symbol `__imp_SDL_free' defined in .idata$5 section in SDL/lib/x64/SDL2.lib(SDL2.dll.b)
SDL/lib/x64/SDL2.lib(SDL2.dll.b):(.text+0x2): relocation truncated to fit: R_X86_64_32 against symbol `__imp_SDL_malloc' defined in .idata$5 section in SDL/lib/x64/SDL2.lib(SDL2.dll.b)
SDL/lib/x64/SDL2.lib(SDL2.dll.b):(.text+0x2): relocation truncated to fit: R_X86_64_32 against symbol `__imp_SDL_iconv_string' defined in .idata$5 section in SDL/lib/x64/SDL2.lib(SDL2.dll.b)
SDL/lib/x64/SDL2.lib(SDL2.dll.b):(.text+0x2): relocation truncated to fit: R_X86_64_32 against symbol `__imp_SDL_wcslen' defined in .idata$5 section in SDL/lib/x64/SDL2.lib(SDL2.dll.b)
SDL/lib/x64/SDL2.lib(SDL2.dll.b):(.text+0x2): relocation truncated to fit: R_X86_64_32 against symbol `__imp_SDL_isspace' defined in .idata$5 section in SDL/lib/x64/SDL2.lib(SDL2.dll.b)
collect2: Fehler: ld gab 1 als Ende-Status zurück
修改
我找到了一个工作场所: 我现在正在使用mingw32来编译我的文件:
i686-pc-mingw32-g++ main.cpp -o main -ISDL/i686-w64-mingw32/include/SDL2/ -LSDL/i686-w64-mingw32/lib -lmingw32 -lSDL2main -lSDl2 -static-libgcc -static-libstdc++
我还将SDL二进制文件复制到我的可执行文件的文件夹中,现在一切正常
答案 0 :(得分:6)
我知道这是一个旧线程,但是为了那些正在努力使用mingw32构建SDL2依赖应用程序的人(就像我已经使用了几周),我想我可能会回复。
有五点最终解决了我遇到的各种错误,包括OP发布的错误。这些要点并不适用于OP的问题,但我想到了一个完整的清单,列出了帮助我使用mingw32努力编译SDL2的其他人的帮助。这些要点如下:
确保编译器和应用程序链接到的所有库都针对相同的体系结构(32位或64位)进行编译。您不能混用32位和64位库和/或编译器。
确保链接到libSDL2main.a。
链接到SDL2库的顺序非常重要。首先是SDL2main,然后是SDL2,之后顺序可能无关紧要,但是我找到的源代码为所有可用的SDL2库提供了特定的顺序,所以我认为我坚持使用它。这是我的来源:
http://www.cplusplus.com/forum/beginner/110753/
下一点适用于使用mingw32从linux到windows的交叉编译(也可能是使用mingw32在windows上进行本机编译):不要链接到.dll,而是链接到.dll.a代替。下载时,每个SDL2库都应该有.dll.a文件,在x86_64-w64-mingw32和i686-w64-mingw32文件夹中(取决于你是在编译64位还是32位应用程序)。
最后,确保链接到libgcc和libstdc ++。如果您计划分发您的应用程序,最好静态链接到这些应用程序,否则您必须使用您的应用程序提供这些库。这个静态链接可以按如下方式完成:-static-libgcc -static-libstdc++
这些应该在mingw32之后立即链接到SDL2库之前:
-lmingw32 -static-libgcc -static-libstdc ++ -lSDL2main -lSDL2(...-任何其他SDL2库)
此步骤的来源是:
http://arrayfire.com/cross-compile-to-windows-from-linux/
最后一点,在OP的最后一次编辑中,他们写了-lSDl2而不是-lSDL2。
我对这一切仍然相当新,所以如果我对上述任何一点都有误,请告诉我,我会编辑我的答案。希望这有助于将来的某个人!