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
cd SDL
git checkout SDL2
brew install git
brew install cmake
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"
)
#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.
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.
You should obtain: SDL2.lib and SDL2main.lib (SDL2-VS2015.7z).
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.
You should obtain: SDL2_image.lib (SDL2_image.lib for VS2015)
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)
You should obtain: SDL2_tff.lib (SDL2_ttf.lib for VS2015)
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)
You should obtain: SDL2_gfx.lib (SDL2_gfx.lib for VS2015)
There is no dependencies!
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