SDL2 is a cross-platform library that gives us the ability to create windows, access input devices and create graphics.
In this guide, we’ll walk through the process of adding it to a C++ project using Xcode on a mac. This includes Macs that use Apple Silicon - M1 and M2 processors.
We’ll be building SDL2 from the source code. This may seem daunting, but the SDL2 code comes with some very helpful scripts that make things much easier.
First things first, we need to download the source code from GitHub.
The latest version of SDL will be available on the releases page on GitHub: https://github.com/libsdl-org/SDL/releases
We want to download the source code version - either as a zip
or tar.gz
 file.
Once downloaded, we can decompress that into any location on our hard drive.
Within finder, we should navigate to the folder that was decompressed - typically it will be called SDL-main
. Within that folder, we should create a new folder, called build
.
Then, right click on the new build
folder and select New Terminal at Folder.
Within the new terminal window, run the command ../configure
The main cause of this error is that the terminal window is not in the correct location. We want the build folder to be inside the folder that was decompressed from the GitHub download. Then, we want the terminal to be open inside that folder.
You can check for this using the command pwd
- the output is likely to be something like:
/Users/your-name/Downloads/SDL-main/build
Specifically, we want the ending of this output to be SDL-main/build
. If that is not the case, ensure you’re creating the correct folder hierarchy and try again
The configure
command will scan your system to better understand how SDL2 should be compiled. It should complete within a few seconds.
When it is complete, run the command make
in the same build
location. This command will take a little longer, as it is compiling SDL2.
When that process completes, we next need to install the software onto our machine. That can be done with the command: sudo make install
Finally, when that completes, we should run the following command, again from the build
folder: sdl2-config --libs --cflags
This command will show exactly where SDL2 was installed. Copy that output somewhere - we will need it later. The output will look something like this:
-L/usr/local/lib -lSDL2
-I/usr/local/include/SDL2 -D_THREAD_SAFE
In summary, the 4 commands we need to run, in order, from the build folder are:
../configure
make
sudo make install
sdl2-config --cflags --libs
With this step complete, it’s time to move over to our XCode project and add SDL2 to it
Next, we need to add SDL2 to the Xcode project, or create a new one if needed.
Once the project is open, we need to open the settings for it. Within the General tab, under the Frameworks and Libraries section, we need to add a new library using the +Â icon.
At the bottom of the popup menu, select Add Other, followed by Add Files.
Next, we need to navigate to the location suggested when we ran sdl2-config --libs --cflags
 earlier.
My output was the following:
-L/usr/local/lib -lSDL2
-I/usr/local/include/SDL2 -D_THREAD_SAFE
We’re interested in the lib
folder, so in my case, the location we want is /usr/local/lib
. Your location may have been different.
Either way, we need to navigate to the folder listed. The path may be hidden, so you will need to press Command + Shift + . (period) to show hidden files.
Once we’re in the location, we want to select the .dylib
file that is a UNIXÂ executable:
After selecting that file, the Framework and Libraries section of the project settings should be updated:
Finally, within the Build Settings tab of our project settings, we need to scroll down to the Search Paths section. There, we need to add the respective SDL locations to the Header Search Paths and Library Search Paths.
Again, these were the paths given by the sdl2-config --libs --cflags
command. We need to put the lib
path in the Library Search Paths section, and the include path in the Header Search Paths section.
In my case, it looks like this, but your paths may have been different:
With everything set up, we should now be able to #include
SDL files in our code, and start using it!
The following is a minimalist main.cpp
file that will test our setup:
#include <SDL.h>
int main() {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* Window { SDL_CreateWindow(
"Hello Window", 0, 0, 800, 300, 0
) };
SDL_Surface* WindowSurface {
SDL_GetWindowSurface(Window)
};
SDL_FillRect(
WindowSurface,
nullptr,
SDL_MapRGB(WindowSurface->format, 40, 40, 40)
);
SDL_UpdateWindowSurface(Window);
SDL_Event event;
while(true) {
SDL_PollEvent(&event);
}
}
If everything is working correctly, we’ll see a window pop up when our program runs:
Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games