SDL 无法在本地环境中编译

SDL doesn't compile in native enviroment

本文关键字:环境 编译 SDL      更新时间:2023-10-16

我试图用SDL编译这段代码

#include <SDL.h>
int main(int argc, char * argv[]){
    return 0;
}

使用

文件本身可以很好地编译
g++ -c main.cpp -ISDL/include

但是在cygwin中使用g++

编译以下命令
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

Thanks in advance

编辑

改变库顺序会产生以下错误

$ 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二进制文件复制到我的可执行文件文件夹中,现在一切正常

我知道这是一个旧的线程,但是为了任何正在努力使用mingw32构建SDL2依赖应用程序的人(就像我已经好几周了),我想我可能会回复。

有五点最终修复了我所面临的各种错误,包括OP发布的错误。这些观点并不都适用于OP的问题,但我认为一个完整的帮助我的列表可能对其他使用mingw32编译SDL2的人有用。这些要点如下:

  1. 确保编译器和你的应用链接到的所有库都是在相同的架构下编译的(32位或64位)。你不能混用32位和64位的库和/或编译器

  2. 确保链接到libSDL2main.a.

  3. 链接到SDL2库的顺序很重要。首先是SDL2main,然后是SDL2,之后的顺序可能无关紧要,但是我找到的一个源代码给出了所有可用的SDL2库的特定顺序,所以我想我应该坚持使用它。这是我的来源:

http://www.cplusplus.com/forum/beginner/110753/

  • 下一点适用于使用mingw32从linux到windows的交叉编译(可能也适用于使用mingw32在windows上的本机编译):不要链接到。dll,而是链接到。dll。相反。应该有。dll。每个SDL2库在下载时提供的文件,在文件夹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。

    我对这一切还很陌生,所以如果我在以上任何一点上有错误,请让我知道,我会修改我的答案。希望这对将来的人有所帮助!