Search:  
Gentoo Wiki

HOWTO_MinGW_and_SDL_CrossCompiling

This article is part of the HOWTO series.
Installation Kernel & Hardware Networks Portage Software System X Server Gaming Non-x86 Emulators Misc

Contents

HowTo set up a cross-compiling environment with mingw32, SDL and KDevelop

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.

Getting a Cross-Compiler

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)."

The first step in getting your own cross-compiler environment is to install a toolchain for the Win32 environment. To do so you have to run:

emerge -va sys-devel/crossdev

This installs crossdev, a little utility that takes care of installing the toolchain, and has replaced the xmingw ebuilds. Next you need to provide a portage-overlay where crossdev can place its ebuilds. It is assumed that you aren't using an overlay. If you already do, see here. To add an overlay execute:

echo 'PORTDIR_OVERLAY="/usr/local/portage"' >> /etc/make.conf

Once you have created the overlay and the according directories execute crossdev -t i686-mingw32. This installs a recent toolchain to /usr/. The libraries and include-files are installed to /usr/i686-mingw32/usr. The compiler is installed to /usr/i686-pc-linux-gnu/i686-mingw32/gcc-bin/4.2.0. 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 crossdev --gcc 3.4.2 -t i686-mingw32

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 /usr/i686-mingw32/usr/. 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.

I suppose there's a way to install the SDL via xmerge, crossdev, whatever but I didn't succeed...

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:

LD with the value: i686-mingw32-ld
LIBSDL_CONFIG with the value: /usr/i686-mingw32/usr/bin/sdl-config
PATH with the value: /usr/i686-mingw32/usr/bin:/usr/i686-mingw32/usr:$PATH
prefix with the value /usr/i686-mingw32

If you're done your screen should look sth. like this:

Image:Kdevelop-CaptainSifff.1.png


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 libtoolize --copy --force 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:

Image:KDevelop-CaptainSifff.2.png

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:

ln -s /usr/i686-mingw32/usr/lib/libopengl32.a /usr/i686-mingw32/usr/lib/libGL.a
ln -s /usr/i686-mingw32/usr/lib/libglu32.a /usr/i686-mingw32/usr/lib/libGLU.a

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.

Then 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 /usr/i686-mingw32/usr/include/SDL .

SDL_Net.dll to /usr/i686-mingw32/usr/bin/ and

libSDL_net.a and libSDL_net.dll.a to /usr/i686-mingw32/usr/lib . 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

References

  1. Free Compilers and Cross-Compilers for Linux and Windows (GCC 4.2.3)
  2. Free Compilers and Cross-Compilers for Linux and Windows (GCC 4.3.0 Initial Results)
  3. The Gentoo MingW HowTo
  4. Cross-Compiling for Windows with KDevelop and MingW
  5. SDL-Documentation regarding cross-compiling
  6. SDL-FAQ
  7. Gentoo Cross-Development Guide
  8. Another tutorial
Retrieved from "http://www.gentoo-wiki.info/HOWTO_MinGW_and_SDL_CrossCompiling"

Last modified: Sat, 14 Jun 2008 10:07:00 +0000 Hits: 6,119