In this tutorial we are making a static build of SDL using Visual Studio.
Why do we want to link SDL statically ?
- The licence zlib is permissive enough.
- There are too many dll files to ship with the app.
Let’s count the number of dll :
One dll for SDL:
SDL2.dll
Six dll for SDL2_image:
SDL2_image.dll libjpeg-9.dll libpng16-16.dll libtiff-5.dll libwebp-4.dll zlib1.dll
Three dll for SDL2_ttf:
SDL2_ttf.dll libfreetype-6.dll zlib1.dll
Seven dll for SDL2_mixer:
SDL2_mixer.dll libFLAC-8.dll libmodplug-1.dll libogg-0.dll libvorbis-0.dll libvorbisfile-3.dll smpeg2.dll
One dll for SDL2_gfx:
SDL2_gfx.dll
So if you use everything there are 17 dll files to include (one zlib1.dll is enough).
But wait there is more:
With visual studio your program is probably compiled with \MD which means the run-time library is a dll. So the user will need to have the package Visual C++ Redistributable for Visual Studio installed to run the app.
The first thing to do is to link the static version of the run-time library with your app, for that change \MD to \MT (for Release) in the Project Properties -> C/C++ -> Code Generation -> Runtime Library
All the SDL libraries are compiled with the flag \MD so we will recompile every library and change \MD to \MT.
Static build of SDL
Compile SDL2 statically
- Download the sources
- Open the visual studio solution (VisualC folder)
- Change the type of project for SDL2 and SDL2main: General -> Configuration Type -> Static Library
- Change \MD to \MT
- Select Release instead of Debug and compile!
You should obtain: SDL2.lib and SDL2main.lib (SDL2-VS2015.zip).
SDL2 dependencies
Here is the list of dependencies to add in the Linker -> Input of the project you want to compile:
winmm.lib imm32.lib version.lib
These libraries are already included in visual studio so it should not be necessary to include them with other lib files.
SDL2_image static
Compile SD2_image
- Download the sources
- Open the visual studio solution (VisualC folder)
- Include the headers of SDL2 in the folder SDL2_image-2.0.1/VisualC/external/include
- Change \MD to \MT and the type of project for SDL2 and SDL2main: General -> Configuration Type -> Static Library
- Delete the Preprocessor definitions (C/C++ -> Preprocessor -> Preprocessor Definitions) with the word “DLL” except for tiff (create comflict with the other format)
- Select Release instead of Debug and compile!
You should obtain: SDL2_image.lib (SDL2_image.lib for VS2015)
SDL2_image dependencies
You will need to compile the libraries in the folder SDL2_image-2.0.1/external/, don’t forget to pick the static version or add \MT if you have a visual studio project.
You can find other resources on how to compile these libraries.
The list of the dependencies are : libpng16.lib, libwebp.lib, libjpeg.lib and zlib.lib (SDL2_image_dep-VS2015.zip).
You need to include these in the linker, if you use tiff image you also need to add libtiff-5.dll (SDL2_image-2.0.1/VisualC/external/lib)
SDL2_ttf static
Compile SD2_ttf
- Download the sources
- Open the visual studio solution (VisualC folder)
- Include the headers of SDL2 in the folder SDL2_ttf-2.0.14/VisualC/external/include
- Change \MD to \MT and the type of project for SDL2 and SDL2main: General -> Configuration Type -> Static Library
- Select Release instead of Debug and compile!
You should obtain: SDL2_tff.lib (SDL2_ttf.lib for VS2015)
SDL2_ttf dependencies
There is only the library freetype to compile statically linked: SDL2_ttf-2.0.14/external/freetype-2.4.12.
You should obtain: libfreetype-6.lib (libfreetype-6.lib for VS2015)
SDL2_mixer static
Compile SD2_mixer
- Download the sources
- Open the visual studio solution (VisualC folder)
SDL2_mixer dependencies
SDL2_gfx static
Compile SD2_gfx
- Download the sources
- Change \MT and the type of the project to Static Library and compile !
You should obtain: SDL2_gfx.lib (SDL2_gfx.lib for VS2015)
SDL2_gfx dependencies
There is no dependencies!
Project Test
When you create a project with all the libraries you should include for the linker:
winmm.lib imm32.lib version.lib SDL2main.lib freetype2412.lib SDL2_ttf.lib zlib.lib libpng16.lib libwebp.lib libjpeg.lib SDL2_image.lib timidity.lib libFLAC_static.lib smpeg.lib libmodplug.lib libogg_static.lib libvorbis_static.lib win_utf8_io_static.lib SDL2.lib libvorbisfile_static.lib SDL2_gfx.lib SDL2_mixer.lib
and have two dll:
smpeg.dll SDL.dll
You can download the test project (with all includes an libs) for VS2015: TestSDL.zip