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, such as jpeg and pngSDL_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 Windows, and then demonstrates how to add them to a project that uses Visual Studio
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 release you download starts with 2
(for example, 2.26.4
)
Once we’ve found the latest version 2 release, Windows users should download the -VC
package of each library:
Once we have our three zip files, we should extract them to a location on our hard drive. It can be helpful to add them to our project directory so that everything our project needs is in one place. However, for the sake of this demo, I have extracted them to c:\sdl
:
Each of the folders includes 3 things we are interested in:
include
directorylib
directorylib
directoryThe lib
directory includes both 32-bit (x86) and 64-bit (x64) versions of the libraries. In this guide we’ll only be setting up the x64 version but, if your project needs 32-bit as well, the steps can be repeated for that configuration.
Let’s walk through how we’d add these libraries to our Visual Studio project. We can add the libraries to an existing project, or create a new project if needed.
Once we have our project open, we need to open the project properties window, available from the top menu under Project > Properties
Within our project properties, we need to change three things:
#include
them in our own files.Under Configuration Properties > VC++ Directories, ensure we’re configuring the x64 version of our project by looking at the Platform dropdown at the top. Then, open the Include Directories list, and click Edit:
Here, we need to add the include directory of each of the three zips we extracted earlier:
Under Configuration Properties > VC++ Directories, open the Library Directories list, and click Edit
Here, we need to add the library directory from each of the three zips we extracted earlier. Because we’re configuring the 64-bit version of our project (x64) we use the x64 directories:
Under Configuration Properties > Linker > Input, open the Additional Dependencies list, and click Edit:
Here, we need to add a list of .lib
 files:
SDL2.lib
SDL2main.lib
SDL2_ttf.lib
SDL2_image.lib
These are file names that should exist within the lib
directories we added to our project in step 2
If everything is set up correctly, the following simple program should now compile successfully. Note that the program may not yet run successfully, but at this point, we just want to ensure it compiles:
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_VIDEO);
IMG_Init(IMG_INIT_PNG);
TTF_Init();
return 0;
}
This error is generally going to be caused if we define our main
function with one of the shorter signatures:
int main() {
// ...
return 0;
}
int main(void) {
// ...
return 0;
}
Normally, the abbreviated main
function would work, but SDL sometimes needs to change the entry point of our program to ensure it is compatible with the platform we’re running it on. The Windows operating system is one such environment where this is necessary,
Therefore, to ensure our code is compatible with SDL, we need to use the full signature for our main
 function:
int main(int argc, char** argv) {
// ...
return 0;
}
This error often indicates that the main
function in our code is not explicitly returning a value:
int main(int argc, char** argv) {
// ...
}
Normally, this would work - the C++ specification states that if our entry function doesn’t return a value, it’s treated as if it returned 0
.
However, SDL needs to rename our main
function behind the scenes to ensure our program is compatible with the platform it’s running on. As such, our main
function needs to adhere to the rules as if it were a regular function, and return an appropriate value:
int main(int argc, char** argv) {
// ...
return 0;
}
Even though our program now compiles successfully, we may be getting errors when we try to run it. Our program may crash and report an error like:
The code execution cannot proceed because SDL2.dll was not found
To fix this, we need to ensure our system can find the SDL libraries our program is using. The three DLL files we need are:
SDL2.dll
SDL2_image.dll
SDL2_ttf.dll
These are included within the zips we downloaded earlier, within the /lib/x64
directory of each zip file. The easiest way we can help our system find these DLLs is to copy them to the same location as the executable that runs our program.
Note that in this image, my project is called SDL2
, so the executable is called SDL2.exe
. Yours will likely be different:
It’s important to remember our compiled project is no longer a single .exe
file - it now includes these .dll
files. When we ship our program, we need to include all of the required .dll
files so our users can run it.
.exe
file?The location where Visual Studio places your executable is called the Output Directory, and is configured from project properties under Configuration Properties > General > Output Directory
By default, the output directory will be within your project directory, in a subdirectory based on the release configuration such as x64\debug
The location will also be listed within your output window when you build the project. For example, the second line in the following output shows the location of the executable that was created:
Build started: Project: SDL2, Config: Debug x64
SDL2.vcxproj -> C:\SDL2\x64\Debug\SDL2.exe
Build: 1 succeeded
Build took 01.580 seconds
There is another mechanism we can use to help our system find the files our programs depend on. Windows maintains a list of locations it will automatically search for objects such as .dll
 files.
It is called the path variable, and it is available under System Properties > Advanced > Environment Variables > Path
Rather than moving the .dll
files next to the executable, we could move them into one of the directories in that variable, such as windows/system32
. Alternatively, we could add a new directory to the path variable, and store our DLLs there.
The main use for this is when the same library is going to be used by multiple executables. Rather than having unnecessary copies of the same DLL, we could store it in a shared location, accessible to all executables that need it.
In this lesson, we covered how to set up the SDL2 library for Windows development using Visual Studio. The key steps are:
With SDL2 set up, you're now ready to start using it in the next chapter!
A step-by-step tutorial on configuring SDL2, SDL_image and SDL_ttf in a Visual Studio C++ project on Windows
Apply what we learned to build an interactive, portfolio-ready capstone project using C++ and the SDL2 library
Apply what we learned to build an interactive, portfolio-ready capstone project using C++ and the SDL2 library
Free, unlimited access