How to Cross-Compile an SDL Application for Win32 Using Linux

Dana Olson - 20060408


I wrote this page because I found it very frustrating to find an easy tutorial and up-to-date documentation on this topic. This is my attempt at helping others as well as myself in the future.

Firstly, if you're on debian or Ubuntu, you do not need to run the famous build-cross.sh script. You can install the MinGW compiler using Synaptic or apt:

debian users run:
su -c apt-get install mingw32
Ubuntu users run:
sudo apt-get install mingw32

Otherwise, you will likely want to get my updated version of the build-cross.sh script. It is current as of Apr. 7th, 2006, and is not guaranteed to work for you. I have submitted it to Sam Lantinga so he may update his official version soon too.

You can download my updated version here: build-cross.sh [TXT]

So now that you have your cross-compiler installed, you need the SDL libraries installed. To do this, I have written a second script to do it all for you. It is fully documented, so if you don't want to run it as a script, just open up the TXT file version and read it in your browser, following each step as you wish.

You can get my SDL install script here: sdl-cross-install.sh [TXT]

Once you have it downloaded, you simply run it like this:
sh sdl-cross-install.sh /path/to/install/to
where /path/to/install/to is the path... you wish to... install... to. :D You could also leave off the path, and the default of /opt/SDL-1.2.9 will be used instead.

Once you are done that install, you will be left with a directory tree wherever you installed to, and it will look like this:
/opt/SDL-1.2.9/ (click to expand)
|-- BUGS
|-- COPYING
|-- INSTALL
|-- Makefile
|-- README
|-- WhatsNew
|-- archives (click to expand)
|   |-- SDL-devel-1.2.9-mingw32.tar.gz
|   |-- SDL_image-devel-1.2.4-VC6.zip
|   |-- SDL_mixer-devel-1.2.6-VC6.zip
|   |-- SDL_net-devel-1.2.5-VC6.zip
|   |-- SDL_ttf-devel-2.0.7-VC6.zip
|   `-- sdlcfgfix.patch
|-- bin (click to expand)
|   `-- i386-mingw32msvc-sdl-config
|-- docs (click to expand)
|   |-- html (click to expand)
|   |   |-- audio.html
|   |   |-- cdrom.html
|   |   |-- event.html
|   |   |-- eventfunctions.html
|   |   |-- eventstructures.html
|   |   |-- general.html
|   |   |-- guide.html
|   |   |-- guideaboutsdldoc.html
|   |   |-- guideaudioexamples.html
|   |   |-- guidebasicsinit.html
|   |   |-- guidecdromexamples.html
|   |   |-- guidecredits.html
|   |   |-- guideeventexamples.html
|   |   |-- guideexamples.html
|   |   |-- guideinput.html
|   |   |-- guideinputkeyboard.html
|   |   |-- guidepreface.html
|   |   |-- guidethebasics.html
|   |   |-- guidetimeexamples.html
|   |   |-- guidevideo.html
|   |   |-- guidevideoopengl.html
|   |   |-- index.html
|   |   |-- joystick.html
|   |   |-- reference.html
|   |   |-- sdlactiveevent.html
|   |   |-- sdladdtimer.html
|   |   |-- sdlaudiocvt.html
|   |   |-- sdlaudiospec.html
|   |   |-- sdlblitsurface.html
|   |   |-- sdlbuildaudiocvt.html
|   |   |-- sdlcd.html
|   |   |-- sdlcdclose.html
|   |   |-- sdlcdeject.html
|   |   |-- sdlcdname.html
|   |   |-- sdlcdnumdrives.html
|   |   |-- sdlcdopen.html
|   |   |-- sdlcdpause.html
|   |   |-- sdlcdplay.html
|   |   |-- sdlcdplaytracks.html
|   |   |-- sdlcdresume.html
|   |   |-- sdlcdstatus.html
|   |   |-- sdlcdstop.html
|   |   |-- sdlcdtrack.html
|   |   |-- sdlcloseaudio.html
|   |   |-- sdlcolor.html
|   |   |-- sdlcondbroadcast.html
|   |   |-- sdlcondsignal.html
|   |   |-- sdlcondwait.html
|   |   |-- sdlcondwaittimeout.html
|   |   |-- sdlconvertaudio.html
|   |   |-- sdlconvertsurface.html
|   |   |-- sdlcreatecond.html
|   |   |-- sdlcreatecursor.html
|   |   |-- sdlcreatemutex.html
|   |   |-- sdlcreatergbsurface.html
|   |   |-- sdlcreatergbsurfacefrom.html
|   |   |-- sdlcreatesemaphore.html
|   |   |-- sdlcreatethread.html
|   |   |-- sdlcreateyuvoverlay.html
|   |   |-- sdldelay.html
|   |   |-- sdldestroycond.html
|   |   |-- sdldestroymutex.html
|   |   |-- sdldestroysemaphore.html
|   |   |-- sdldisplayformat.html
|   |   |-- sdldisplayformatalpha.html
|   |   |-- sdldisplayyuvoverlay.html
|   |   |-- sdlenablekeyrepeat.html
|   |   |-- sdlenableunicode.html
|   |   |-- sdlenvvars.html
|   |   |-- sdlevent.html
|   |   |-- sdleventstate.html
|   |   |-- sdlexposeevent.html
|   |   |-- sdlfillrect.html
|   |   |-- sdlflip.html
|   |   |-- sdlfreecursor.html
|   |   |-- sdlfreesurface.html
|   |   |-- sdlfreewav.html
|   |   |-- sdlfreeyuvoverlay.html
|   |   |-- sdlgetappstate.html
|   |   |-- sdlgetaudiostatus.html
|   |   |-- sdlgetcliprect.html
|   |   |-- sdlgetcursor.html
|   |   |-- sdlgeterror.html
|   |   |-- sdlgeteventfilter.html
|   |   |-- sdlgetgammaramp.html
|   |   |-- sdlgetkeyname.html
|   |   |-- sdlgetkeystate.html
|   |   |-- sdlgetmodstate.html
|   |   |-- sdlgetmousestate.html
|   |   |-- sdlgetrelativemousestate.html
|   |   |-- sdlgetrgb.html
|   |   |-- sdlgetrgba.html
|   |   |-- sdlgetthreadid.html
|   |   |-- sdlgetticks.html
|   |   |-- sdlgetvideoinfo.html
|   |   |-- sdlgetvideosurface.html
|   |   |-- sdlglattr.html
|   |   |-- sdlglgetattribute.html
|   |   |-- sdlglgetprocaddress.html
|   |   |-- sdlglloadlibrary.html
|   |   |-- sdlglsetattribute.html
|   |   |-- sdlglswapbuffers.html
|   |   |-- sdlinit.html
|   |   |-- sdlinitsubsystem.html
|   |   |-- sdljoyaxisevent.html
|   |   |-- sdljoyballevent.html
|   |   |-- sdljoybuttonevent.html
|   |   |-- sdljoyhatevent.html
|   |   |-- sdljoystickclose.html
|   |   |-- sdljoystickeventstate.html
|   |   |-- sdljoystickgetaxis.html
|   |   |-- sdljoystickgetball.html
|   |   |-- sdljoystickgetbutton.html
|   |   |-- sdljoystickgethat.html
|   |   |-- sdljoystickindex.html
|   |   |-- sdljoystickname.html
|   |   |-- sdljoysticknumaxes.html
|   |   |-- sdljoysticknumballs.html
|   |   |-- sdljoysticknumbuttons.html
|   |   |-- sdljoysticknumhats.html
|   |   |-- sdljoystickopen.html
|   |   |-- sdljoystickopened.html
|   |   |-- sdljoystickupdate.html
|   |   |-- sdlkey.html
|   |   |-- sdlkeyboardevent.html
|   |   |-- sdlkeysym.html
|   |   |-- sdlkillthread.html
|   |   |-- sdllistmodes.html
|   |   |-- sdlloadbmp.html
|   |   |-- sdlloadwav.html
|   |   |-- sdllockaudio.html
|   |   |-- sdllocksurface.html
|   |   |-- sdllockyuvoverlay.html
|   |   |-- sdlmaprgb.html
|   |   |-- sdlmaprgba.html
|   |   |-- sdlmixaudio.html
|   |   |-- sdlmousebuttonevent.html
|   |   |-- sdlmousemotionevent.html
|   |   |-- sdlmutexp.html
|   |   |-- sdlmutexv.html
|   |   |-- sdlnumjoysticks.html
|   |   |-- sdlopenaudio.html
|   |   |-- sdloverlay.html
|   |   |-- sdlpalette.html
|   |   |-- sdlpauseaudio.html
|   |   |-- sdlpeepevents.html
|   |   |-- sdlpixelformat.html
|   |   |-- sdlpollevent.html
|   |   |-- sdlpumpevents.html
|   |   |-- sdlpushevent.html
|   |   |-- sdlquit.html
|   |   |-- sdlquitevent.html
|   |   |-- sdlquitsubsystem.html
|   |   |-- sdlrect.html
|   |   |-- sdlremovetimer.html
|   |   |-- sdlresizeevent.html
|   |   |-- sdlsavebmp.html
|   |   |-- sdlsempost.html
|   |   |-- sdlsemtrywait.html
|   |   |-- sdlsemvalue.html
|   |   |-- sdlsemwait.html
|   |   |-- sdlsemwaittimeout.html
|   |   |-- sdlsetalpha.html
|   |   |-- sdlsetcliprect.html
|   |   |-- sdlsetcolorkey.html
|   |   |-- sdlsetcolors.html
|   |   |-- sdlsetcursor.html
|   |   |-- sdlseteventfilter.html
|   |   |-- sdlsetgamma.html
|   |   |-- sdlsetgammaramp.html
|   |   |-- sdlsetmodstate.html
|   |   |-- sdlsetpalette.html
|   |   |-- sdlsettimer.html
|   |   |-- sdlsetvideomode.html
|   |   |-- sdlshowcursor.html
|   |   |-- sdlsurface.html
|   |   |-- sdlsyswmevent.html
|   |   |-- sdlthreadid.html
|   |   |-- sdlunlockaudio.html
|   |   |-- sdlunlocksurface.html
|   |   |-- sdlunlockyuvoverlay.html
|   |   |-- sdlupdaterect.html
|   |   |-- sdlupdaterects.html
|   |   |-- sdluserevent.html
|   |   |-- sdlvideodrivername.html
|   |   |-- sdlvideoinfo.html
|   |   |-- sdlvideomodeok.html
|   |   |-- sdlwaitevent.html
|   |   |-- sdlwaitthread.html
|   |   |-- sdlwarpmouse.html
|   |   |-- sdlwasinit.html
|   |   |-- sdlwmgetcaption.html
|   |   |-- sdlwmgrabinput.html
|   |   |-- sdlwmiconifywindow.html
|   |   |-- sdlwmsetcaption.html
|   |   |-- sdlwmseticon.html
|   |   |-- sdlwmtogglefullscreen.html
|   |   |-- thread.html
|   |   |-- time.html
|   |   |-- video.html
|   |   `-- wm.html
|   `-- index.html
|-- docs.html
|-- include (click to expand)
|   |-- SDL -> .
|   |-- SDL.h
|   |-- SDL_active.h
|   |-- SDL_audio.h
|   |-- SDL_byteorder.h
|   |-- SDL_cdrom.h
|   |-- SDL_copying.h
|   |-- SDL_cpuinfo.h
|   |-- SDL_endian.h
|   |-- SDL_error.h
|   |-- SDL_events.h
|   |-- SDL_getenv.h
|   |-- SDL_image.h
|   |-- SDL_joystick.h
|   |-- SDL_keyboard.h
|   |-- SDL_keysym.h
|   |-- SDL_loadso.h
|   |-- SDL_main.h
|   |-- SDL_mixer.h
|   |-- SDL_mouse.h
|   |-- SDL_mutex.h
|   |-- SDL_name.h
|   |-- SDL_net.h
|   |-- SDL_opengl.h
|   |-- SDL_quit.h
|   |-- SDL_rwops.h
|   |-- SDL_syswm.h
|   |-- SDL_thread.h
|   |-- SDL_timer.h
|   |-- SDL_ttf.h
|   |-- SDL_types.h
|   |-- SDL_version.h
|   |-- SDL_video.h
|   |-- begin_code.h
|   `-- close_code.h
|-- lib (click to expand)
|   |-- SDL_image.lib
|   |-- SDL_mixer.lib
|   |-- SDL_net.lib
|   |-- SDL_ttf.lib
|   |-- libSDL.a
|   |-- libSDL.dll.a
|   |-- libSDL.la
|   `-- libSDLmain.a
|-- runtime (click to expand)
|   |-- README-SDL.txt
|   |-- SDL.dll
|   |-- SDL_image.dll
|   |-- SDL_mixer.dll
|   |-- SDL_net.dll
|   |-- SDL_ttf.dll
|   |-- jpeg.dll
|   |-- libpng13.dll
|   `-- zlib1.dll
|-- share (click to expand)
|   `-- aclocal (click to expand)
|       `-- sdl.m4
`-- test (click to expand)
    |-- COPYING
    |-- Makefile.am
    |-- Makefile.in
    |-- README
    |-- acinclude.m4
    |-- aclocal.m4
    |-- autogen.sh
    |-- checkkeys.c
    |-- configure
    |-- configure.in
    |-- graywin.c
    |-- icon.bmp
    |-- loopwave.c
    |-- missing
    |-- moose.dat
    |-- picture.xbm
    |-- sail.bmp
    |-- sample.bmp
    |-- sample.wav
    |-- testalpha.c
    |-- testbitmap.c
    |-- testblitspeed.c
    |-- testcdrom.c
    |-- testcpuinfo.c
    |-- testdyngl.c
    |-- testendian.c
    |-- testerror.c
    |-- testgamma.c
    |-- testgl.c
    |-- testhread.c
    |-- testjoystick.c
    |-- testkeys.c
    |-- testlock.c
    |-- testoverlay.c
    |-- testoverlay2.c
    |-- testpalette.c
    |-- testsem.c
    |-- testsprite.c
    |-- testtimer.c
    |-- testtypes.c
    |-- testver.c
    |-- testvidinfo.c
    |-- testwin.c
    |-- testwm.c
    |-- threadwin.c
    `-- torturethread.c
As you can see, the DLL files that you will need for Windows EXEs are inside the runtime directory.

Now, if your app uses configure, you can invoke it in this manner:
PATH=/opt/SDL-1.2.9/bin:$PATH ./configure --host=i586-mingw32msvc --build=i386-linux
make
This presumes that you used the default path to install to. If you didn't, then simply change it how you like.

If you are not using debian or Ubuntu, but the cross-build.sh script, then you might need to set your environment variables. I am not sure exactly, because I don't use it. If you can clarify this, let me know. Here is the set of variables I would probably set:
export CC=/opt/cross-tools/bin/i386-mingw32msvc-gcc
export CXX=/opt/cross-tools/bin/i386-mingw32msvc-g++
export LD=/opt/cross-tools/bin/i386-mingw32msvc-ld 
export AR=/opt/cross-tools/bin/i386-mingw32msvc-ar
export AS=/opt/cross-tools/bin/i386-mingw32msvc-as
export NM=/opt/cross-tools/bin/i386-mingw32msvc-nm
export STRIP=/opt/cross-tools/bin/i386-mingw32msvc-strip
export RANLIB=/opt/cross-tools/bin/i386-mingw32msvc-ranlib
export DLLTOOL=/opt/cross-tools/bin/i386-mingw32msvc-dlltool
export OBJDUMP=/opt/cross-tools/bin/i386-mingw32msvc-objdump
export RESCOMP=/opt/cross-tools/bin/i386-mingw32msvc-windres
export CFLAGS=
Here is an example Makefile that I used on one of my basic projects that does not use autotools: Makefile.mingw [TXT]

Here is a simple script I wrote to make the clean/configure/make process simpler for Win32 cross-compilation (no more remembering the configure parameters and such). Just put it in your project directory, edit the "PATH=/opt/SDL-1.2.9/bin" portion to point at your SDL library installation path (don't forget the bin directory!) and then run it to cross-compile: make-win32.sh [TXT]

Further notes:
- You can use upx to compress EXE and DLL files.
- You can uses nsis to create Windows application installers.


[BACK]


References:
http://www.libsdl.org/extras/win32/cross/README.txt
http://www.wxwidgets.org/wiki/index.php/Cross-Compiling_Under_Linux
http://ubuntuforums.org/showthread.php?t=156632

After I wrote this page, I was given more links that could have been very helpful to me, and I include them here because they are good documentation, and might help others:
https://dev.njh6.de/wiki/index.php/Doc:MinGW-SDL_Cross_Compiling#SDL
https://dev.njh6.de/wiki/index.php/Any2deb
https://dev.njh6.de/wiki/index.php/Cross_Compiling_for_Win32



[BACK]