How do I cross-compile an SDL program that uses SDL_image?

I am on Ubuntu, trying to compile a C/SDL2 program to Windows using MinGW. I have included the development libraries and includes of both SDL2 and SDL_image like this:

CFLAGS =  -Wall -Werror -Wextra -pedantic
WLIBS = -lmingw32 \
-I ~/Downloads/SDL2-2.30.3/i686-w64-mingw32/include \
-L ~/Downloads/SDL2-2.30.3/i686-w64-mingw32/lib \
-I ~/Downloads/SDL2_image-2.6.0/i686-w64-mingw32/include \
-L ~/Downloads/SDL2_image-2.6.0/i686-w64-mingw32/lib \
-I ~/Downloads/mingw64/include \
-L ~/Downloads/mingw64/lib
	i686-w64-mingw32-gcc ./src/files/* -o game.exe ${WLIBS} ${CFLAGS};

But I am running into an issue with the include files, I am getting this error:

In file included from ./src/files/game.c:3:
/home/gnxrly/Downloads/SDL2_image-2.6.0/i686-w64-mingw32/include/SDL2/SDL_image.h:32:10: fatal error: SDL.h: No such file or directory
   32 | #include "SDL.h"
      |          ^~~~~~~
compilation terminated.

I’m only linking -lSDL2, -lSDL2_image and -lpng for Linux, so those are the Windows equivalents. Do I also need to find the devel files for SDL1 for SDL_image to work?

  gcc main.c -o main -lSDL2

  Folgende Datei muss im Stamm-Ordner des Projektes sein libSDL2main.a
  x86_64-w64-mingw32-gcc main.c -o main.exe -lmingw32 -lSDL2main -lSDL2 -mwindows -I/usr/local/include -L/usr/local/bin -L.

#include <SDL2/SDL.h>
#include <SDL2/SDL_main.h>

int main(int argc, char *argv[]) {
    SDL_Window *window = SDL_CreateWindow("An SDL2 window", 0, 0, 640, 480, SDL_WINDOW_OPENGL);
    return 0;

I have the following demo, which can be cross-compiled under Linux. These two paths still need to be adjusted -I/usr/local/include -L/usr/local/bin
And you also need to have the following lib: libSDL2main.a

I don’t understand. I don’t think anything is wrong with my program because it compiles and works flawlessly on Linux with gcc. The error is coming from the SDL_main.h file in the devel include that I downloaded for the cross-compilation.
I tried adding #include <SDL2/SDL_main.h> to all my files, as well as adding -lSDL2main -lSDL2 -mwindows to the flags, but the issue persists.
Also, where do I find libSDL2main.a and how do I link it?

According to the error message, what’s missing here is adding the SDL2 include dir itself to the search path, like -I ~/Downloads/SDL2-2.30.3/i686-w64-mingw32/include/SDL2

If linker errors turn up afte fixing this, they may be due to missing libSDL2main, which is part of the download so -lSDL2main should generally work, but see also [SDL2.x] WinMain vs. main - #5 by Daniel_Gibson for the correct order of linking SDL2 and SDL2main with MinGW

I read the forum you have linked and changed the order of the flags

WLIBS = -I ~/Downloads/SDL2-2.30.3/i686-w64-mingw32/include \
-L ~/Downloads/SDL2-2.30.3/i686-w64-mingw32/lib \
-I ~/Downloads/SDL2_image-2.6.0/i686-w64-mingw32/include \
-L ~/Downloads/SDL2_image-2.6.0/i686-w64-mingw32/lib \
-I ~/Downloads/mingw64/include \
-L ~/Downloads/mingw64/lib \
-lmingw32 -lSDL2main -lSDL2 -mwindows

I also swapped the C flags position so the libs can be at the end of the command

	i686-w64-mingw32-gcc ./src/files/* -o game.exe ${CFLAGS} ${WLIBS};

I am still getting the same error.

you should’ve read my whole reply

According to the error message, what’s missing here is adding the SDL2 include dir itself to the search path, like -I ~/Downloads/SDL2-2.30.3/i686-w64-mingw32/include/SDL2

1 Like

My bad, I didn’t notice that.
This error is now gone, thanks a lot for your help!

Great, I hope it works now :slight_smile:

I hoped too, but there’s another problem

/usr/bin/i686-w64-mingw32-ld: /tmp/cc6JCKhW.o:init.c:(.text+0x5da): undefined reference to `IMG_Load'

I am getting this error for each time that I’m calling this function. I tried including the SDL2 directory inside the SDL2_image devel include folder like this

-I ~/Downloads/SDL2_image-2.6.0/i686-w64-mingw32/include/SDL2 \
-I ~/Downloads/SDL2_image-2.6.0/i686-w64-mingw32/include \

But it hasn’t worked

I just had to add the -lSDL2_image flag, and the error diappeared.

@Daniel_Gibson This insight useful for me :grinning: