Cross-compiling a SDL3 application from linux to windows with cmake
Well a few days ago I made a little test project, and I wanted to compile the project for windows.
We are going to use that project as a demo. See the source code here: github
Installing toolchain
To compile to windows, we will need the mingw toolchains, here is how you install it on arch linux:
$ sudo pacman -S mingw-w64-binutils mingw-w64-headers mingw-w64-gcc # Compilers
$ yay -S mingw-w64-cmake mingw-w64-zlib # Cmake, not 100% sure zlib is needed
Compiling dependencies
For this project we need SDL3 and SDL3_image, but the AUR doesn't have these packages yet, thus we need to compile them by ourselfs
First, let's compile SDL
$ git clone https://github.com/libsdl-org/SDL
$ cd SDL
$ mkdir build && cd build
$ x86_64-w64-mingw32-cmake .. # Switch to i686-w64-mingw32-cmake for 32 bit
$ make
$ make install # the cmake automatically configures the install path to be inside /usr/x86_64-w64-mingw32
Now let's compile SDL_image
$ git clone https://github.com/libsdl-org/SDL_image
$ cd SDL_image
$ mkdir build && cd build
$ x86_64-w64-mingw32-cmake .. # The same as SDL
$ make
$ make install
Not hard huh. But when I first tried, I actually made a custom cmake toolchain etc., which was stupid and wasted a bunch of time :P
Compiling project
Now on to compiling the project!
If you wan't to compile your project, you prob need to change CMakeLists.txt: see CMakeLists.txt, everything in if(WINDOWS)
is for windows
$ git clone https://github.com/cheyao/TileMap
$ cd TileMap
$ mkdir build && cd build
$ x86_64-w64-mingw32-cmake .. # Still the same!
$ make
Yay! Now we have a exe, now let's try it out with wine!
$ wine TileMap.exe
0124:err:module:import_dll Library libgcc_s_seh-1.dll (which is needed by L"F:\\home\\user\\Developer\\TileMap\\build\\TileMap.exe") not found
0124:err:module:import_dll Library libstdc++-6.dll (which is needed by L"F:\\home\\user\\Developer\\TileMap\\build\\TileMap.exe") not found
0124:err:module:import_dll Library SDL3.dll (which is needed by L"F:\\home\\user\\Developer\\TileMap\\build\\TileMap.exe") not found
0124:err:module:import_dll Library SDL3_image.dll (which is needed by L"F:\\home\\user\\Developer\\TileMap\\build\\TileMap.exe") not found
0124:err:module:loader_init Importing dlls for L"F:\\home\\user\\Developer\\TileMap\\build\\TileMap.exe" failed, status c0000135
Oh no! A bunch of dll modules missing! We need to copy them from /usr/x86_64-w64-mingw32/ to the build directory:
$ cp /usr/x86_64-w64-mingw32/bin/libgcc_s_seh-1.dll /usr/x86_64-w64-mingw32/bin/libstdc++-6.dll /usr/x86_64-w64-mingw32/bin/SDL3.dll /usr/x86_64-w64-mingw32/bin/SDL3_image.dll /usr/x86_64-w64-mingw32/bin/libwinpthread-1.dll /usr/x86_64-w64-mingw32/bin/libssp-0.dll .
And now we have out code running!