Glusoft

Compile SDL3 on MacOS with Cmake

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 want to have the last version of SDL3 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 :
mkdir build
cd build
ccmake ..
press c (for configure)
You'll need to set the line 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 SDL3 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(SDL3Project)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

find_package(SDL3 REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} SDL3::SDL3)

After you have saved the CmakeLists.txt inside a new directory for example SDL3Project.
We need to create a sample project in c++ for that use this simple example diplaying a green rectangle.
Inside a main.cpp file copy the following code, the file need to be in the same directory of the CMakeLists.txt file

#include <SDL3/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("SDL3 Project",640, 480, 0);
    if (win == nullptr) {
        std::cerr << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }

    SDL_Renderer* ren = SDL_CreateRenderer(win, NULL);
    if (ren == nullptr) {
        std::cerr << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
        SDL_DestroyWindow(win);
        SDL_Quit();
        return 1;
    }

    SDL_Event e;
    bool quit = false;

    // Define a rectangle
    SDL_FRect greenSquare {270, 190, 100, 100};

    while (!quit) {
        while (SDL_PollEvent(&e)) {
            if (e.type == SDL_EVENT_QUIT) {
                quit = true;
            }
        }

        SDL_SetRenderDrawColor(ren, 0, 0, 0, 255); // Set render draw color to black
        SDL_RenderClear(ren); // Clear the renderer

        SDL_SetRenderDrawColor(ren, 0, 255, 0, 255); // Set render draw color to green
        SDL_RenderFillRect(ren, &greenSquare); // Render the rectangle
        
        SDL_RenderPresent(ren); // Render the screen
    }

    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);
    SDL_Quit();

    return 0;
}    
Now you have two files in your folder:

You can build and test you project! You can do the same thing as before:

mkdir build
cd build
ccmake ..
press c (for configure)
You'll need to set the line CMAKE_BUILD_TYPE to Release and set SDL3_DIR 
to the path of the folder where you build the SDL3 library.
press c (for configure)
press g (for Generate)
make

You need to have somehting like this in the terminal during the cmake configure : Cmake Configure step