SDL 无法在本地环境中编译
SDL doesn't compile in native enviroment
我试图用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的人有用。这些要点如下:
-
确保编译器和你的应用链接到的所有库都是在相同的架构下编译的(32位或64位)。你不能混用32位和64位的库和/或编译器
-
确保链接到libSDL2main.a.
-
链接到SDL2库的顺序很重要。首先是SDL2main,然后是SDL2,之后的顺序可能无关紧要,但是我找到的一个源代码给出了所有可用的SDL2库的特定顺序,所以我想我应该坚持使用它。这是我的来源:
下一点适用于使用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。
我对这一切还很陌生,所以如果我在以上任何一点上有错误,请让我知道,我会修改我的答案。希望这对将来的人有所帮助!
- 在编译时为运行时设置环境变量
- 选择特定版本的 Visual Studio 命令行工具包,并根据特定版本的C++运行时环境编译文件
- Stringstream编译错误仅发生在linux环境中
- CMake:如何在Visual Studio环境中将依赖于模式的编译标志传递给nvcc
- 在编译时使用 gnu++11 过滤值列表,不使用 stdlib(Arduino 环境)
- 在源代码中使用环境变量(编译时间)
- 在CodeBlocks上编译C 中的Oracle数据库环境时的错误
- LNK2001在X64环境中的编译代码上看到的错误.但是,该代码在X86环境中填充了罚款
- 在Linux环境下用MYSQL.h编译C++程序
- 使用 Code::Blocks、plink 和 makefile 进行远程编译时无法访问环境变量
- Visual Studio 2008环境变量在编译时使用
- 编译C++.lib文件使它们成为C#环境的DLL
- 我可以定义一个环境变量并在条件编译中使用它吗?
- 如何在64位x86环境下编译32位x86应用程序
- SDL 无法在本地环境中编译
- 对本机环境中使用 SDL 编译进行'WinMain'的未定义引用
- 使用OpenWRT工具链为OpenWRT Linux环境交叉编译POCO库
- 如何安装适用于Mingw Linux编译环境的BerkeleyDB
- 是什么原因导致此缓冲区溢出警告超过64位VS2012编译环境
- 在64位Debian环境下编译32位qt源代码