Lazy Foo' Productions

Setting up SDL 2 on Visual Studio 2019 Community Edition

Last Updated: Sep 17th, 2022

1)First thing you need to do is download SDL 2 headers and binaries. You will find them on the SDL GitHub, specifically on this page. You'll want to download the Visual C++ development libraries.
SDL download page

Open the zip archive and there should be a folder called SDL2-2.something.something. Copy the contents of the folder and put it anywhere you'd like. I recommend putting it in a folder that you dedicate to holding all your development libraries for Visual C++. For these tutorials I'm putting them in a directory I created called C:\vclib

2)Start up Visual Studio and create a new empty C++ project.
new project page
Give your project/solution whatever name you'd like and place it where ever you'd like.

3)Go download the source for lesson 01 and extract the source file. Right click on the source files folder in your solution, and then add the source file you downloaded.
add existing item

4)Now, if your default build setting is Debug x86, you may need to change it:
x64 configuration
For the rest of this tutorial, we will be assuming you are building for Debug x64 so make sure your configuation is set to Debug x64. Because libraries are different per configuation, you will need to add SDL to every configuation you plan on using. So if you want to build for Release x64 or Debug x86, you will need to add SDL2 to each configuation.

5)Build your solution and you should get the following error:

Cannot open include file: 'SDL.h': No such file or directory

This means Visual C++ cannot find the SDL header files and you need to add the SDL include folder to the Visual C++ include directories. Go to project properties:
project properties
Go to Configuration Properties -> VC++ Directories -> Include Directories -> Edit.
edit include directories

And then add the include directory from the SDL development folder we extracted.
add include folder

6)Try to build your solution again and you should get a bunch of errors including:

unresolved external symbol SDL_GetError referenced in function SDL_main

The header file tells the compiler what the SDL functions are, not where to find them. The library file tells the compiler where they are and we need to tell it to use the SDL library file. Go to Configuration Properties -> Linker -> Additional Dependencies -> Edit.
edit additional dependencies

Now add
SDL2.lib; SDL2main.lib;

sdl libs

7)Try to build your solution again and you should get a new error:

cannot open file 'SDL2.lib'

While we did tell Visual C++ to use the SDL library files, we didn't tell it where to find it. Add the library directory like you did the include directory
edit library directories

Make sure to add the library that matches your build configuation. If your building for x64, make sure to use the x64 library.
add lib directory

8)Build and your application should build, but try to run it and you'll get this error:

The code execution cannot proceed because SDL2.dll was not found.

This is because your application needs SDL2.dll to run but can't find it. Windows uses environment variables to define where to look for dll files. To edit the PATH environment variable, go into Windows Settings and search for edit the system environment variables:
edit env vars

Click environment variables and under System Variables select Path and click Edit
edit path vars

Then click new, then browse to add the lib directory for your build configuation:
add path

Restart Visual Studio so Visual C++ can get the updated path variable, start your program and it should run.

Now that you have SDL 2 compiling, it's time to go onto part 2 of the tutorial.