MinGW and SDL CrossCompiling

Important
This data is very out of date. Try using i686-pc-mingw32 instead of i686-mingw32.

Abstract
This HowTo shows you the necessary steps to set up mingw32, SDL and KDevelop, so it's only a matter of a click to either compile your project for Win32- or for linux-targets.

What's Cross-Compiling ?
From mingw.org : How can I build a cross compiler?: "Cross-compiling refers to building applications for one platform, with development occuring on another platfrom (i.e. compiling MS-Windows executables from within a Linux environment, or vice-versa)."

Getting a Cross-Compiler
The first step in getting your own cross-compiler environment is to install a toolchain for the Win32 environment. To do so, use the crossdev script, available from Portage.

Once you have created the overlay and the according directories execute:

This installs a recent toolchain to. The libraries and include-files are installed to. The compiler is installed to.

Somewhere on the Gentoo forums I found the information that the support for exception-handling in shared libraries in mingw32 with gcc-4 is broken. If you need this you should build your toolchain with

Installing SDL
Now that your toolchain is installed, you need to add the SDL to the environment. You need the mingw32 Development Libraries from them. Download the current version. Then extract at least the folders bin, include, lib and share to. Take care that the installed files have the proper permissions. If you get errors like:

libSDL.la is not a proper libtool archive

that's most likely because of wrong permissions.

You may also install from the source, if it's your preference.

note: this routine is typically what you need to install libraries for your crossdev environment. You may wish to run first to look for other options you are interested in setting. Also pay attention to the output of configure, it will often warn you about missing dependencies, especially so for SDL_image...

Setting up KDevelop
It is assumed that you already have KDevelop installed and a small SDL-project ready(e.g. the "Simple SDL Program" template from KDevelop. If you don't, you have to take care that the right options for including the libraries of SDL appear in your configure.in). Now click the buttons "Project", then "Project Options" and then "Configure Options". In the upper part you see an input-field called "Configuration". There you type "Win32" and click to the right of it on "add" (Every other name to distinguish your projects configurations is also possible, e.g. "mingw"). The fields in the "General" tab should read: Configure arguments --target=i686-mingw32 --host=i686-mingw32 --build=i686-linux --with-sdl-config=/usr/i686-mingw32/usr/bin/sdl-config

Build directory Win32

Top source directory and C/C++ preprocessor flags(CPPFLAGS) should be left empty, except your project needs it.

Linker flags(LDFLAGS) -L/usr/i686-mingw32/usr/lib -mwindows

Then you have to add various environment variables: If you're done your screen should look sth. like this:



In the "C"-tab you have to set "Compiler-command(CC)" to i686-mingw32-gcc

Accordingly in the "C++"-tab you have to set "Compiler-command(CXX)" to i686-mingw32-g++

Now click "OK" to save your changes and let KDevelop run "Autoconf & Friends" for the Win32 target. If you get an error about a failed Gentoo sanity-check, you have to execute

in the top-level-directory of your project.

That's it! You are now able to switch between linux-targets and Win32-targets simply by switching to the according configuration in the "Project"->"Build Configuration" dialog and press "F8" to start Compiling. See here:



The final binaries don't need any additional dlls(no cygwin.dll, etc.), they are ready to be deployed on any Windows-system (I guess on Win98 onwards...). Since this HowTo is about the SDL, the binaries using the SDL of course need the SDL.dll on their Windows-system.

Adding OpenGL Support
If you try to compile a project which uses OpenGL, your Compiler will bail out with something like:

/usr/libexec/gcc/i686-mingw32/ld: cannot find -lGL collect2: ld returned 1 exit status

That's because in Windows the OpenGL libraries have different names:libGL is libopengl32 and libGLU is libglu32. If you don't fear a name-collision, it's the simplest to create symlinks, as KDevelop has not yet (never?) support for different libraries for different configuration targets. So do something like:

Adding SDL_Net support
First you need to compile SDL_Net to obtain the proper libraries for mingw32. You can obtain the SDL_net source from here and extract it somewhere. You compile it analogous to your own win32 - programs: First you set up the necessary environment variables:

export prefix=/usr/i686-mingw32}

export PATH=/usr/i686-mingw32/usr/bin:/usr/i686-mingw32/usr:$PATH

export LIBSDL_CONFIG=/usr/i686-mingw32/usr/bin/sdl-config

export LD=i686-mingw32-ld

Then you invoke configure like this: ./configure --target=i686-mingw32 --host=i686-mingw32 --build=i686-linux --with-sdl-config=/usr/i686-mingw32/usr/bin/sdl-config

Note that configure will warn you, that it can't compile the example chat-client. That's because you propably won't have the necessary libraries installed. Next you execute make like this:

make LDFLAGS=-L/usr/i686-mingw32/usr/lib/ CFLAGS="-O -I/usr/i686-mingw32/usr/include/SDL"

After you've successfully compiled the sources you copy SDL_Net.h from the archive to. SDL_Net.dll to and libSDL_net.a and libSDL_net.dll.a to. Don't forget to tell KDevelop in the Automake Manager that your program needs to be linked to SDL_net(You've already done this for compiling on linux).

Miscellaneous
If you want to compile Auto-Tools based projects with your new toolchain, you should take a look at cross-configure and cross-make. If you miss OpenMP support in your mingw32 toolchain you are lost. There's no support yet for openmp with native win32 threads. You might want to try to get something working with win32-pthreads instead.