How to Install SDL2 for Game Development

Context

SDL2 is a widely use framework - set of libraries - to create OpenGL contexts and handle input/output for graphics programming. It is also a good library to learn when you are exploring game development.

The challenge

There’s a variety of environments nowadays that software developers can use. Sometimes, a person may use more then one operating system for development, or want to ensure that the program is at least on Desktop multi-platform ready.

When creating your environment to program with SDL2, and perhaps other libraries later on like OpenAL for handling sounds for instance, if you do your setup focusing only on a single operating system, later you may have big trouble (meaning hours of extra work) to be able to work with your code in another platform.

In this tutorial I will show you how to install SDL2 in the three main operating systems out there:

  • MS Windows
  • MacOS
  • Linux

Knowing how to instal SDL2 in these three platforms will get you a better toolset and will enable you to work with your code in any of these platforms without having too much trouble (setting up libraries is never a trouble-free action independent of which operating system you are using).

Before we begin

You should be aware of two things.

1 - Making use of functionality from SDL2 in your code

In order to make use of SDL2 functionality in your code you will need to inport the corresponding header file for your need.

Most of the SDL2 functionality can be found in the SDL2.h header file. However, depending on your needs you’ll have to require different headers.

A header file contains the method signatures that you can use in your code at development time.

2 - Linking used functionality to the actual implemention

When you compile your program that is using methods from SDL2, the compiler will link that methods you used to the corresponding implementation in the compiled SDL2 library. Therefore, you will also need to install and know where the SDL2 library resides in your machine so you can reference it when compiling your code.

Installing SDL2 on Windows

The installation of SDL2 on Windows can be done in many ways and none of them is super easy I’d say.

I’ve tried a couple of different ways, and the one I like the most is to insall it within the MSYS2 environment as it will also allow you to easily install other tools you will need in order to compile and build your programs later on.

Here are the steps I recommend.

Step 1: Install MSYS2

Donwload the installer from msys2.org and install it on C:\dev\msys2.

Run it once the installation is completed.

On the MSYS2 shell, update the database of libraries:

pacman -Syu

Note: MSY2 comes with pacman, a package manager.

Step 2: Install the basic dev libraries

pacman -S --needed base-devel mingw-w64-x86_64-toolchain

Close, and re-open the MSYS2 terminal again to continue the process.

Step 3: Install the SDL2 library

pacman -S mingw64/mingw-w64-x86_64-SDL2

Close and re-open the MSYS2 shell.

Note: You can browse the avialable packages on this page https://packages.msys2.org/package/ in case you want to install other libraries too.

Step 4: Checking the installation

The library will be installed to C:\dev\msys64\mingw64 folder or mingw32 in case you are using a 32bit Windows version (which should be rare in these days).

Check the location of the SDL2 header files

You should be able to find inside the C:\dev\msys64\mingw64\include\SDL2 all the SDL2 headers files.

Check the location of the SDL2 lib files

You should be able to find inside the C:\dev\msys64\mingw64\bin all the SDL2 dll files you will need to run your program. The DLL files will need to be located together with your executable file so that your program runs correctly.

Note: The small grapic programs I make - mostly small games - are all dynamically linked. You can start with this approach too. It will work just fine. Check a brief explanation of what dynamic linking is here on this article.

Installing on MacOS

Installing SDL2 on a MacOS based system is way more straightforward with the help of Homebrew.

I made a video tutorial on Youtube on how to use SDL2 with VSCode on Mac in case you are interested.

How to use SDL2 with VSCode on a Mac

Here are the steps I suggest.

Step 1: Install SDL2 using Homebrew

brew install sdl2

Step 2: Checking the installation

The library will be installed under /urs/local/Cellar, check if SDL2 is available there.

The header files can be found under /usr/include/SDL2, check if the headers are available there.

Installing on Linux

Installing on Linux is also not a problem. With the help of the apt package manager, installing SDL2 becomes an easy task.

Here are the steps I suggest.

Step 1: Install SDL2 using the apt package manager

sudo apt install libsdl2-dev

Step 2: Checking the installation

The header files will be installed by default into /usr/include.

💡If you need to search in the apt-cache you can use:

apt-cache search libsdl2 | grep dev

Final notes

These are the ways I use to get SDL2 installed on my machines nowadays. It is not perfect but it works well.

On Windows I usually don’t use links to the headers. I copy them into the project. I do the same with the libraries (DLLs), copying them into the build folder where I generete the .exe file.

On Mac and Linux, I usually use symbolic links to the folders containing the headers and the library objects.

I’m rethinking this process to create one that is the same across these different platforms. At the moment, I’m learning towards just copying what I need to my project in an automated way based on platform.

I’ve tried a couple of package managers and automated installers but didn’t like much any so far. Perhaps a simple script will solve all these problems without having to learn how a new package manager works or without having to deal with symbolic links manually.

Stay tunned! I’ll write a new blog post when I finally make my mind on which approach to use.

I hope this tutorial helped you in some way.