Blay
Blay

Reputation: 151

Cross-compiling c++ with sdl on linux

I'm on Arch Linux, and I have a C++ SDL2 program, contained in single main.cpp file, and I compile it for Linux with such command:

g++ main.cpp -lSDL2 -lSDL2_image

Now I wanna compile it for windows. Any advice on what should I do?

Upvotes: 1

Views: 1740

Answers (2)

user2097260
user2097260

Reputation: 41

I'd go for cmake really.. SDL2 ships with a CMakeLists.txt and it's as simple as running this from your build folder.

cmake.exe ..

cmake.exe --build .

EDIT: if you want to cross-compile, you need MinGW and the addition of the mingw flags to the cmake generator

cmake \
  -D CMAKE_C_COMPILER=/path/to/wingw/gcc \
  -D CMAKE_CXX_COMPILER=/path/to/mingw/g++ \
  -G "MinGW Makefiles" ..

Upvotes: 1

HolyBlackCat
HolyBlackCat

Reputation: 96430

I suggest my own tool, quasi-msys2, which lets you reuse the precompiled SDL2 for MinGW provided by MSYS2 (and more).

Install Clang, LLD, make, wget, tar, zstd, gpg.

git clone https://github.com/HolyBlackCat/quasi-msys2
cd quasi-msys2/
make install _gcc _SDL2 _SDL2_image
env/shell.sh
win-clang++ main.cpp `pkg-config --cflags --libs sdl2 SDL2_image`

This should produce a.exe, which you can test using wine a.exe (or just ./a.exe, after running env/shell.sh).


How to do this manually:

For completeness, SDL2 itself distributes precompiled binaries for MinGW, meaning the manual setup is not hard. Any tutorial for MinGW should work.

Install MinGW from your package manager.

Download and unpack SDL2-devel-??-mingw.zip and SDL2_image-devel-??-mingw.zip.

Specify the paths to the directories with .a files using -L... and to .h files using -I.... Add -lmingw32 -lmingw32 -mwindows -lSDL2main -lSDL2 -lSDL2_image to the linker flags.

Follow this troubleshooting guide if you get stuck.

Upvotes: 3

Related Questions