Search code examples
c++sdlsdl-image

Include SDL_image in mingw build on ubuntu


I'm trying to build a windows executable for a C++ application I've made that uses SDL2, and SDL_Image. I've seemingly managed to include the SDL libraries and headers just fine, but now I'm trying to include the SDL_Image ones. The command I'm currently using is as follows:

i686-w64-mingw32-gcc -lSDL2main -lSDL2 -I ~/SDL/SDL2-devel-2.0.22-mingw/SDL2-2.0.22/i686-w64-mingw32/include -L ~/SDL/SDL2-devel-2.0.22-mingw/SDL2-2.0.22/i686-w64-mingw32/lib  -o main32.exe main.cpp

But this gives me the error

main.cpp:4:10: fatal error: SDL2/SDL_image.h: No such file or directory
    4 | #include <SDL2/SDL_image.h>
      |          ^~~~~~~~~~~~~~~~~~
compilation terminated.

I am aware that SDL_Image is a separate plugin, and I have already installed it.

What directory(s) do I need to specify to include SDL_Image in the build?

I'm on Ubuntu 22.

Edit: I have found the correct directories, and I now have the following command:

i686-w64-mingw32-gcc -lmingw32 -lSDL2main -lSDL2 -lSDL2_image \
-I /home/nick/SDL/SDL2-devel-2.0.22-mingw/SDL2-2.0.22/i686-w64-mingw32/include \
-L /home/nick/SDL/SDL2-devel-2.0.22-mingw/SDL2-2.0.22/i686-w64-mingw32/lib \
-I /home/nick/SDL/SDL2_image-devel-2.6.0-mingw/SDL2_image-2.6.0/i686-w64-mingw32/inclulde \
-L /home/nick/SDL/SDL2_image-devel-2.6.0-mingw/SDL2_image-2.6.0/i686-w64-mingw32/lib   \
-o main32.exe main.cpp

However, the command still gives me the same error. I have checked, and I am sure that the header file it cannot find is is the correct place.


Solution

  • SDL2_Image is a plugin for SDL2, and needs to be downloaded separately. You also need to specify -I and -L for it, the same way you did for the SDL2 itself.

    Also you forgot -lmingw32 (must be -lmingw32 -lSDL2main -lSDL2 in this exact order), plus -lSDL2_image after those.


    As always, a shameless plug: I've made quasi-msys2, a cross-compilation environment that mimics (and is based on) MSYS2, but works on Linux.

    Here's how you'd use it:

    # Download Clang, LLD. Then:
    git clone https://github.com/HolyBlackCat/quasi-msys2
    cd quasi-msys2
    make install _gcc _SDL2 _SDL2_image
    env/shell.sh
    

    Then:

    $CXX main.cpp -o main `pkg-config --cflags --libs sdl2 SDL2_image`