SDL doesn't compile in native enviroment

2019-08-31 04:22发布

问题:

I'm trying to compile this piece of code with SDL

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

The file itself compiles fine with

g++ -c main.cpp -ISDL/include

But compiling with following command in cygwin using g++

g++ -o test main.o  -lSDL2 -lSDL2main -L SDL/lib/x64

produces this huge error... it seems to me like a problem of SDL itself...

$ 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

Edit

Altering the library order gives following error

$ 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

Edit

I've found a workarround: I'm now using mingw32 to compile my files:

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++

I also copied the SDL binary to the folder of my executable and now everything works just fine

回答1:

I know this is an old thread, but for the sake of anyone who is struggling to build SDL2 reliant apps using mingw32 (like I have been for weeks), I thought I might reply.

There were five points that finally fixed the various errors I have faced, including the ones the OP have posted. These points don't all apply to the OP's question, but I figure a complete list of what helped me may be useful to anyone else who struggles to compile against SDL2 using mingw32. These points were as follows:

  1. Make sure both the compiler and all the libraries that your app links to are compiled for the same architecture (32 bit or 64 bit). You can't mix 32 bit and 64 bit libraries and/or compilers.

  2. Make sure to link to libSDL2main.a.

  3. The order in which you link to the SDL2 libraries is important. SDL2main comes first, then SDL2, and then after that the order might not matter, however a source I found gave a specific order for all SDL2 libraries that works, so I figured I'd stick with it. This was my source:

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

  1. This next point applies to cross-compilation from linux to windows using mingw32 (and maybe also native compilation on windows using mingw32): Don't link to the .dll, but to a .dll.a instead. There should be .dll.a files supplied with each SDL2 library when you download it, within the folders x86_64-w64-mingw32 and i686-w64-mingw32 (depending on whether you are compiling a 64 bit or 32 bit app).

  2. Finally, make sure to link to libgcc and libstdc++. If you are planning to distribute your app, it is preferable to statically link to these, otherwise you will have to supply these libraries with your app. This static linking can be done as follows: -static-libgcc -static-libstdc++ These should be linked to right after mingw32, and before the SDL2 libraries like so:

    -lmingw32 -static-libgcc -static-libstdc++ -lSDL2main -lSDL2 (...-any other SDL2 libraries)

My source for this step is here:

http://arrayfire.com/cross-compile-to-windows-from-linux/

As a final note, in the OP's last edit, they wrote -lSDl2 instead of -lSDL2.

I am still fairly new to all this, so if I am mistaken on any of the above points, please let me know and I will edit my answer. Hope this helps someone in the future!