Glusoft

Static build of SDL2 on MacOS

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 you can install git with:

brew install git

Install Cmake

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 cmake
If you simply want to have the source you can also download iton 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.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.

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;
}