0

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.).

4

2 回答 2

1

您应该使用pkg-config --cflags sdl2pkg-config --libs sdl2来获取您需要的所有 cflags 和库。(顺便说一句。不包括SDL2/SDL.h而只包括SDL.h并使用pkg-config --cflags值来设置你的包含路径,如果你想更便携)

除了使用pkg-config,您还可以使用sdl2-config

如果这对您的设置也没有帮助 - 请查看随 mxe 提供的 sdl2 示例。将您的代码添加到其中,并通过在您的 mxe 结帐中运行make sdl2来尝试(重新)在 mxe 中编译 sdl2 。(https://github.com/mxe/mxe/commit/c3624cdefb7ff0c4b69316c7c1b740b97f55e1db

于 2016-03-03T13:44:01.960 回答
0

我已经安装了 MXE,/opt/mxe/以下对我有用(我试图编译的文件是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`

确保所有可执行文件的路径正确(gcc以及pkg-config),将--cflags参数放在一个之前--libs,确保您键入sdl2(小写的 sdl)和SDL2_image(大写的 SDL),使用-mwindows-lmingw32使用-lopengl32而不是-lgl

于 2016-03-03T15:58:16.500 回答