In this lesson, we’ll get SDL installed, along with two extensions which we’ll need later in this chapter.
SDL
is a cross-platform library that allows us to create windows, access input devices, and create graphics.SDL_image
is an extension that allows us to work with images in all the common formats (jpeg, png, etc)SDL_ttf
is an extension that we can use to render text at run-timeThis setup guide downloads the libraries that have been precompiled for use on a Mac, and then demonstrates how to add them to projects that use either Xcode or CMake
The libraries can be downloaded from the official GitHub release pages, here:
SDL
: https://github.com/libsdl-org/SDL/releasesSDL_Image
: https://github.com/libsdl-org/SDL_image/releasesSDL_TTF
: https://github.com/libsdl-org/SDL_ttf/releasesImportant Note: these release pages may include preview versions of SDL3. This course is using SDL2, so ensure the releases you download starts with 2
(for example, 2.30.2
)
Once we’ve found the latest version 2 release, Mac users should download the .dmg
package of each library:
Once we have our three .dmg
files, each one will contain a folder we need:
SDL2.framework
SDL2_image.framework
SDL2_ttf.framework
We need to copy all of these folders to the /Library/Frameworks
location on our hard drive:
From here, we need to add SDL to our project. How we do that varies depending on our choice of IDE. Below, I’ve included guides for Xcode or, alternatively, any editor that supports CMake.
Let’s walk through how we’d add these libraries to an Xcode project. Within the general settings of our project, under the "Frameworks and Libraries" section, we need to click the + icon to add each new framework.
Within that window, under Add Other… click Add Files
Navigate to our frameworks directory under /Library/Frameworks/SDL.framework, and open the SDL2
alias.
This should add it to our Frameworks and Libraries section. Repeat these steps for SDL2_image
and SDL2_ttf
Our settings should look like this:
Next, within the Build Settings tab, we need to update our search paths to ensure Xcode and our compiled program can find all of our dependencies. These paths are split across four different configuration options:
The search bar on the top right may help us find each one.
Note that some of these paths may have been prepopulated when we added the libraries, so we may not need to add all of these paths. Either way, we want our final configuration to include the following values:
/Library/Frameworks
/Library/Frameworks
/Library/Frameworks/SDL2.framework/Headers
/Library/Frameworks/SDL2_image.framework/Headers
/Library/Frameworks/SDL2_ttf.framework/Headers
/Library/Frameworks/SDL2.framework/Versions/A
/Library/Frameworks/SDL2_ttf.framework/Versions/A
/Library/Frameworks/SDL2_image.framework/Versions/A
With that, everything should be set up and we’re ready to get building!
We’ve included some test code at the bottom of this lesson which will let us verify everything was installed correctly.
CMake is a cross-platform configuration management standard where we define what we want in plain text files. The CMake application can then use these text files to generate project files for a wide variety of IDEs, such as the .xcodeproj
files used by Xcode, or the .vcxproj
files used by Visual Studio.
Some IDEs directly use CMake files as their primary way of managing our project’s configuration. An example that works on macOS is CLion.
If our IDE is compatible with CMake
for configuration management, our setup is much simpler. CMake configurations use a CMakeLists.txt
file. A minimalist example might look something like the following example:
# CMake uses # to denote comments, similar to how
# C++ uses //
# What is the minimum cmake version that is
# required to understand this file?
cmake_minimum_required(VERSION 3.16)
# What language are we using?
set(CMAKE_CXX_STANDARD 20) # C++20
# What is the name and version of our project
project(Sandbox VERSION 1.0.0)
# Our project has an executable that is generated
# from a single source file called main.cpp
add_executable(Sandbox main.cpp)
This CMakeLists.txt
file indicates we’re making a C++23 project called "Sandbox", and it currently only has one source file - main.cpp
The most important line here is the call to project()
. In my case, I called the project "Sandbox", but yours can be different.
Somewhere in our CMakeLists.txt
, after the call to project()
, we need to add the SDL libraries and include directories to the project we defined.
The completed file will look something like this:
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 23)
project(Sandbox VERSION 1.0.0)
add_executable(Sandbox main.cpp)
# Variables storing SDL framework locations
set(SDL2
/Library/Frameworks/SDL2.framework)
set(SDL2_image
/Library/Frameworks/SDL2_image.framework)
set(SDL2_ttf
/Library/Frameworks/SDL2_ttf.framework)
target_link_libraries(
Sandbox PRIVATE
${SDL2}/Versions/A/SDL2
${SDL2_image}/Versions/A/SDL2_image
${SDL2_ttf}/Versions/A/SDL2_ttf
)
target_include_directories(
Sandbox PRIVATE
${SDL2}/Versions/A/Headers
${SDL2_image}/Versions/A/Headers
${SDL2_ttf}/Versions/A/Headers
)
Note that you will need to replace "Sandbox" with your project name. The name in project()
, add_executable()
, target_link_libraries()
, and target_include_directories()
must be identical in all 4 locations.
If everything is installed correctly, the following simple program should compile and execute successfully:
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
int main() {
SDL_Init(SDL_INIT_VIDEO);
IMG_Init(IMG_INIT_PNG);
TTF_Init();
}
Our program will just run and immediately close, but that’s good enough. As long as it was compiled successfully, everything has been set up correctly, and we’re ready to get started!
Setting up SDL2 on macOS involves downloading the precompiled framework files and integrating them into your C++ project.
Whether you use Xcode or CMake, a few configuration steps will get you ready to start building cross-platform applications with SDL2:
This step-by-step guide shows you how to set up SDL2 in an Xcode or CMake project on macOS
Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games