c++sdlmingw-w64

issue with mingw-w64 and SDL2 linking


I am trying to follow an SDL tutorial and compile a c++ file that uses SDL. I am on windows 10, and was originally using MinGW.

Past a certain point I realized my MinGW installation was having issues unrelated to SDL, so I switched to MinGW-w64 instead, which cleared up the non-SDL issue, but now when I try to compile anything with SDL it can't find lSDL2 or lSDL2main. It does, however, skip over various files saying that they are incompatible.

I suspect it might be a 32 bit/64 bit incompatibility, but I'm not sure how this is happening, as even when I explicitly make sure to use the 32 bit g++ compiler, the issue persists. For reference, the command I put into the command prompt is

 x86_64-w64-mingw32-g++  04_key_presses.cpp                 
-IC:\mingw_dev_lib\include\SDL2 -LC:\mingw_dev_lib\lib -w                    
-Wl,subsystem,windows -lmingw32 -lSDL2main -lSDL2 -o key_presses

and the error messages are

C:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:\mingw_dev_lib\lib/libSDL2main.a when searching for -lSDL2main
C:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:\mingw_dev_lib\lib\libSDL2main.a when searching for -lSDL2main
C:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:\mingw_dev_lib\lib/libSDL2main.a when searching for -lSDL2main
C:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL2main
C:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:\mingw_dev_lib\lib/libSDL2.dll.a when searching for -lSDL2
C:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:\mingw_dev_lib\lib/libSDL2.a when searching for -lSDL2
C:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:\mingw_dev_lib\lib\libSDL2.a when searching for -lSDL2
C:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:\mingw_dev_lib\lib/libSDL2.dll.a when searching for -lSDL2
C:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:\mingw_dev_lib\lib/libSDL2.a when searching for -lSDL2
C:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL2
collect2.exe: error: ld returned 1 exit status 

Solution

  • Your compiler is x86_64-w64-mingw32-g++. That's the 64-bit compiler. The 32-bit compiler is i686-w64-mingw32-g++.exe. The 64-bit linkage that you are attempting with:

    x86_64-w64-mingw32-g++  04_key_presses.cpp \                
    -IC:\mingw_dev_lib\include\SDL2 -LC:\mingw_dev_lib\lib -w \                   
    -Wl,subsystem,windows -lmingw32 -lSDL2main -lSDL2 -o key_presses
    

    skips your incompatible 32-bit SDL2 libraries.

    The 64-bit compiler can build either 64- or 32-bit targets, building 64-bit by default. To target 32-bit, pass -m32 for both compilation and linkage. As you doing both with one command you need only pass it once:

    x86_64-w64-mingw32-g++  04_key_presses.cpp -m32 \                
    -IC:\mingw_dev_lib\include\SDL2 -LC:\mingw_dev_lib\lib -w \                   
    -Wl,subsystem,windows -lmingw32 -lSDL2main -lSDL2 -o key_presses