Glusoft

Static build of SDL

Static build of SDL2 on MacOs with Cmake

Download the last version on the github repository of SD2
If you want to have the last version of SDL2 you can pull the master with git:
In a terminal, create a directory where you want to download SDL, with mkdir then :

git clone https://github.com/libsdl-org/SDL.git
After the code is cloned you need to navigate inside the SDL directory :
cd SDL
and change the branch because if you stay on the master you will build SDL3 instead of SDL2:
git checkout SDL2
mkdir build cd build ccmake .. press c (for configure) You'll need to set the line SDL_STATIC to ON and CMAKE_BUILD_TYPE to Release. press c (for configure) press g (for Generate) make -j6 (-j indicate the number of core you want to use to make the project, I have 6 core on my machine). You can install the library on your system with : sudo make install If the make is successful you should have : Copy the file SDL/cmake/sdlfind.cmake inside the build directory of SDL (SDL/build) Copy the folder include in the parent folder of SDL inside this directory include you should have an SDL2 directory. Thiis SDL2 directory is not complete it's missing the SDL_config.h generated with make you should find it in the SDL/build/include-config-release directory, copy this file inside the other include/SDL2 folder with all the files. If you don't have git installed you can install it with Homebrew:
brew install git
If you simply want to have the source you can also download iton github After downloading the project you will need to generate the project to compile, and for that make this step easy. To install it with homebrew :
brew install cmake

Make a static project on MacOs

For this project we will use cmake to handle de makefile fo that we need to create config file for it a CmakeList.txt:

cmake_minimum_required(VERSION 3.10)
    project(StaticProject)
    
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED True)
    
    set(SDL2_USE_STATIC_LIBS ON)
    find_package(SDL2 REQUIRED)
    
    message(STATUS "SDL2 include directories: ${SDL2_INCLUDE_DIRS}")
    include_directories(${SDL2_INCLUDE_DIRS})
    
    add_executable(${PROJECT_NAME} main.cpp)
    
    message(STATUS "SDL2 libraries path: ${SDL2_DIR}")
    
    target_link_libraries(${PROJECT_NAME} 
        ${SDL2_DIR}/libSDL2.a
        "-framework Cocoa"
        "-framework IOKit"
        "-framework CoreFoundation"
        "-framework CoreVideo"
        "-framework CoreAudio"
        "-framework AudioToolbox"
        "-framework CoreHaptics"
        "-framework GameController"
        "-framework CoreServices"
        "-framework Metal"
        "-framework Foundation"
        "-framework QuartzCore"
        "-framework ForceFeedback"
        "-framework Carbon"
    )
To explain more thiis config we create a project named StaticProject, we use the prrevious build of SDL2 by requiring the user to set the path of the package. Then this path allows to find the SDL2_INCLUDE_DIRS wiith all the headers. We display a message to be sure if the path is correct. In this project we simply have one fiile main.cpp a simple SDL App displaying a green sqaure. The more complex step step when static linking is when linking the dependencies of libSDL2.a for that we have different base frameworks in MacOs. This list fo framework can also change I am on Mac Somona.
After creating this will we need an example of main.cpp to execute :
        #include <SDL.h>
        #include <iostream>
            
        int main(int argc, char* argv[]) {
            if (SDL_Init(SDL_INIT_VIDEO) != 0) {
                std::cerr << "SDL_Init Error: " << SDL_GetError() << std::endl;
                return 1;
            }
            
                SDL_Window *win = SDL_CreateWindow("Hello SDL", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
                if (win == nullptr) {
                    std::cerr << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
                    SDL_Quit();
                    return 1;
                }
            
                SDL_Renderer *renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
                if (renderer == nullptr) {
                    std::cerr << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
                    SDL_DestroyWindow(win);
                    SDL_Quit();
                    return 1;
                }
            
                bool running = true;
                SDL_Event event;
            
                while (running) {
                    while (SDL_PollEvent(&event)) {
                        if (event.type == SDL_QUIT) {
                            running = false;
                        }
                    }
            
                    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
                    SDL_RenderClear(renderer);
            
                    SDL_Rect greenSquare = { 270, 190, 100, 100 }; 
                    SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); 
                    SDL_RenderFillRect(renderer, &greenSquare);
            
                    SDL_RenderPresent(renderer);
                }
            
                SDL_DestroyRenderer(renderer);
                SDL_DestroyWindow(win);
                SDL_Quit();
            
                return 0;
            }
            
    

In this tutorial we are making a static build of SDL using Visual Studio.

Why do we want to link SDL statically ?

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

You should obtain: SDL2.lib and SDL2main.lib (SDL2-VS2015.7z).

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

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

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

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

SDL2_mixer dependencies

SDL2_gfx static

Compile SD2_gfx

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.7z