How to statically compile an SDL game on Windows

Sec picture Sec · Sep 22, 2008 · Viewed 17k times · Source

I have been trying to produce a statically linked "single binary" version of my game for windows. I want to link with sdl, sdl_image and sdl_mixer which in turn pull in a few support libraries. Unfortunately I haven't found a way to get them all to compile and link using cygwin/mingw/gcc. As far as I can tell all existing public versions are only shared libraries / dlls.

Please note that I'm not talking about licencing here. The source will be open thus the GPL/LGPLness of sdl is not relevant.

Answer

Alex Lyman picture Alex Lyman · Feb 8, 2009

When compiling your project, you need to make just a couple changes to your makefile.

  • Instead of sdl-config --libs, use sdl-config --static-libs
  • Surround the use of the above-mentioned sdl-config --static-libs with -Wl,-Bstatic and -Wl,-Bdynamic. This tells GCC to force static linking, but only for the libraries specified between them.

If your makefile currently looks like:

SDLLIBS=`sdl-config --libs`

Change it to:

SDLLIBS=-Wl,-Bstatic `sdl-config --static-libs` -Wl,-Bdynamic

These are actually the same things you should do on Unix-like systems, but it usually doesn't cause as many errors on Unix-likes if you use the simpler -static flag to GCC, like it does on Windows.