
Static build of SDL2 on MacOS
How to compile sdl2 statically with cmake or xcode or directly on macos. Making a simple static project can be helpful when making a game to avoid dependencies.
- SDL2, CMake, Homebrew, git
Installation of Homebrew for git and Cmake
What is Homebrew?
Homebrew is a free and open-source software package management system, siimply you will be able to install most of the software and librairies with one command line. If you don’t have homebrew installed you can install it with one command line on the official website Homebrew
Install git
After brew is installed on macOS you can install git with:
brew install gitInstall Cmake on macOS
After downloading the project you will need to generate the project to compile, and for that CMake make this step easy. To install it with homebrew, same thing:
brew install cmakeIf you simply want to have the source you can also download source on github
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.gitAfter the code is cloned you need to navigate inside the SDL directory :
cd SDLand change the branch because if you stay on the master you will build SDL3 instead of SDL2:
git checkout SDL2mkdir build
cd build
ccmake ..- press
c(for configure) - You’ll need to set the line
SDL_STATICtoONandCMAKE_BUILD_TYPEtoRelease. - press
c(for configure) - press
g(for Generate) make -j6(-jindicate 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 installIf the make is successful you should have :
libSDL2.alibSDL2main.alibSDL2_test.a
Copy the file SDL/cmake/sdlfind.cmake inside the static 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. This 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.
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 CMakeLists.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 test the static build of SDL2 on MacOS :
#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;
}