cmingwcross-compilingsdl-2mxe

Multiple "Undefined reference" errors with MXE and SDL2


I am trying to compile a OpenGL program which uses SDL for context creation and image loading. When I compile it on Linux natively, it compiles and runs without error. I have installed MXE in /opt/mxe and have checked that the SDL2 directory (which contains headers) is in /opt/mxe/usr/i686-w64-mingw32.static/include/ while the corresponding libraries (libSDL.a, etc.) are in /opt/mxe/usr/i686-w64-mingw32.static/lib/.

The included header files are <SDL2/SDL.h> and <SDL2/SDL_image.h>

I am trying to compile the said program using

i686-w64-mingw32.static-gcc 5_transformation.c -I/opt/mxe/usr/i686-w64-mingw32.static/include -L/opt/mxe/usr/i686-w64-mingw32.static/lib -lmingw32 -lSDL2main -lSDL2 -lSDL2_image -lGLEW -lopengl32 -lm -mwindows

However, it gives multiple undefined reference errors: [http://pastebin.com/JaadTNnv

]1

Many of these errors seem related to Direct3D and such, which I am not using (but SDL2 might, internally). Do I have to link against them too? Am I even doing this right? (MXE page doesn't seems to have any detailed instructions).

PS. This question, or it's variants, seem to be pretty popular across internet. However, I have already followed their instructions. My main function is defined as int main(int argc, char *argv[]), I am linking against -lmingw32, -lSDL2main and -mwindows, I have made sure that the path in GCC's arguments are correct, I tried putting i686-w64-mingw32.static-sdl-config --cflags --libs and i686-w64-mingw32.static-pkg-config SDL_image --cflags --libs in arguments instead of explicit linking (that simply gave undefined reference for SDL functions like SDL_GL_CreateContext and IMG_Load and said that Package SDL_image was not found in the pkg-config search path.).


Solution

  • I had installed MXE in /opt/mxe/ and the following worked for me (the file I was trying to compile was 5_transformation.c):

    /opt/mxe/usr/bin/i686-w64-mingw32.static-gcc 5_transformation.c -mwindows `/opt/mxe/usr/bin/i686-w64-mingw32.static-pkg-config --cflags sdl2` `/opt/mxe/usr/bin/i686-w64-mingw32.static-pkg-config --cflags SDL2_image` -lmingw32 -lGLEW -lopengl32 -lm `/opt/mxe/usr/bin/i686-w64-mingw32.static-pkg-config --libs sdl2` `/opt/mxe/usr/bin/i686-w64-mingw32.static-pkg-config --libs SDL2_image`
    

    Make sure to get the path to all executables correct (gcc as well as pkg-config), put the --cflags argument before --libs one, make sure that you types sdl2 (sdl in small case) and SDL2_image (SDL in caps), use -mwindows, -lmingw32 and use -lopengl32 instead of -lgl