Breakpoint demolog, day 17: building for windows (from linux)

Windows and Linux executables, rotating a triangle and generating a sine wave audio stream

Today I decided to do something different. And I thought: Okay, I'll try and sort out this compiling for windows issue, so that I can focus on the rest of more important things without having to worry about it any more.

And guess what...? I think I managed to do it :-)

Roughly, these are the steps I followed, just in case someone else wants to compile for windows with opengl and sdl support, using [ubuntu] linux:

  • install mingw (I did it with Synaptic Package Manager so that I didn't have to go through the ./configure and ./make hell)
  • get the dev libraries from sdl for windows/mingw --this file-- or check their downloads page
  • uncompress the file and locate the bin, include and lib folders within, and...
    • copy bin/SDL.dll to the folder where your win32 .exe will go -- so that the exe can pick and load the dll when executing
    • copy the include and lib folders to a folder in your project's src folder -- or anywhere you've got access to! For example: project/src/libs/sdl/
  • now in your main.cpp, include SDL's header files like this: cpp #include <SDL/SDL.h> #include <SDL/SDL_opengl.h>
  • And the makefile could be something like this too: WINDOWS_SDL = ./libs/sdl/windows windows: i586-mingw32msvc-g++ -I$(WINDOWS_SDL)/include -L$(WINDOWS_SDL) \ main.cpp $(WINDOWS_SDL)/libSDL.dll.a -o ../test.exe\ -lmingw32 -lSDLmain -lSDL -mwindows -lopengl32 -Wl,-R.

(ugly hard line returns added by me so that you can see all parameters in one go).

This assumes you are invoking make from the src folder. The output will go to the parent folder. But well, the most important things that need to be highlighted are the bunch of switches you need to include in order to get the program to link:

-I$(WINDOWS_SDL)/include -- this tells the compiler where to look for additional header files. This way it can find <SDL/SDL.h> and you do not need to modify the file or add #define's when building for linux.

-L$(WINDOWS_SDL) -- pretty much the same but tells the compiler to look for static libraries in the WINDOWS_SDL folder too

$(WINDOWS_SDL)/libSDL.dll.a -- links the library into our binary!

-lmingw32 -lSDLmain -lSDL -mwindows -lopengl32 -Wl,-R. -- these are magic - remove any of them and the linker will complain about missing symbols ;)

I still have to test this in a real windows machine; so far I have just tried with Wine. The output is a little jerky and a bit slower than the native (64 bit) counterpart, but I guess this will do much better than compiling in Virtual Box and testing with Wine :D

I'll try to upload a test .exe when I test it on a real windows box myself, so that you can test it and see if it works in your computer (if you want, of course!). It's ages since I compiled anything for win, and I truly wonder whether it will work with Vista/7... my last demo for Windows was done in 2005 and XP was in full glory back then :P