Go back 2019-04-27

Setting Up SDL2 with CMake

Installing CMake

Most common distributions have cmake available on their package manager repostories:

# Debian based
sudo apt install cmake

# Arch
pacman -S cmake

Install SDL2 libraries

I only know about the debian based ones, if you are on another distro you should look them up.

sudo apt install libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-net-dev libsdl2-ttf-dev libsdl2-gfx-dev

Directory Structure

Here is a common directory structure when using cmake to find packages:

├── cmake
│   ├── FindSDL2.cmake
│   ├── FindSDL2_mixer.cmake
│   └── FindSDL2_net.cmake
├── CMakeLists.txt
└── src
    └── main.cpp

You can find the cmake files to find SDL2 and it’s components here

Or you can use this simple command:

cd cmake
wget https://raw.githubusercontent.com/aminosbh/sdl2-cmake-modules/master/FindSDL2{,_gfx,_image,_mixer,_net,_ttf}.cmake

Creating the CMakeLists.txt file

cmake_minimum_required(VERSION 3.13)

# Needed so that cmake uses our find modules.

find_package(SDL2 REQUIRED)
find_package(SDL2_net REQUIRED)
find_package(SDL2_mixer REQUIRED)
find_package(SDL2_image REQUIRED)
find_package(SDL2_gfx REQUIRED)
find_package(SDL2_ttf REQUIRED)



add_executable(MyProject ${SOURCE_FILES})
target_link_libraries(MyProject SDL2::Main SDL2::Net SDL2::Mixer SDL2::Image SDL2::TTF SDL2::GFX)

And thats it! Now you can remove the SDL2 components you don’t want to use.


Following the standard cmake procedure:

mkdir build
cd build
cmake ..
make -j${nproc}