How to Set Up SDL with Visual Studio

Whether you're a novice C++ programmer or a veteran software developer, graphics programming is an educational and rewarding experience. Simple DirectMedia Layer is a C library (compatible with C++) that allows simple, low-level access, to...
Method 1 of 10:

Configuring Visual Studio

  1. How to Set Up SDL with Visual Studio Picture 1How to Set Up SDL with Visual Studio Picture 1
    Highlight step or sub-step. Highlight step or sub-step you expect to do and then do it. See as example picture above.
  2. Download Visual Studio 2019. If you have not done so you can download it from https://www.visualstudio.com/downloads.
  3. How to Set Up SDL with Visual Studio Picture 2How to Set Up SDL with Visual Studio Picture 2
    Check the Desktop development with C++ workload box as you download. If necessary scroll down the page.
Method 2 of 10:

Downloading SDL

  1. How to Set Up SDL with Visual Studio Picture 3How to Set Up SDL with Visual Studio Picture 3
    Create folder GL. In directory (disk) C:, right click > select New > Folder > type GL < hit Enter.
  2. How to Set Up SDL with Visual Studio Picture 4How to Set Up SDL with Visual Studio Picture 4
    Download the last version of SDL2. Right click on the following link and select Open Link in New Window https://www.libsdl.org/download-2.0.php. Scroll to the bottom of the page and find Development Libraries. Click the version just below "Windows:" (see the picture above). Today it is SDL2-devel-2.0.10-VC.zip (Visual C++ 32/64-bit).
    1. In downloading window you have the folder SDL2-2.0.10, (or the last version). Click it > right click > select Copy. (Or, alternatively, click it and drag into C:GL).
    2. Navigate to C:GL. Inside GL, right click > select Paste.
    3. Click on name SDL2-2.0.10, (or the last version), and rename to SDL2. Now in folder GL you have folder SDL2.
Method 3 of 10:

Creating Visual Studio Project

  1. How to Set Up SDL with Visual Studio Picture 5How to Set Up SDL with Visual Studio Picture 5
    Create an empty project. .
    1. If Visual Studio 2019 is not opened. Open it > Click Create a new project > Empty Project > Next.
      1. In "Project name" text field type: Project-0.
      2. Next to "Location" text field click ... > Navigate to C: > GL > click Select folder. Now "Location" is C:GL.
      3. Check "Place solution and project in the same directory" > click Create. Wait till Visual Studio 2019 instance appears.
    2. If Visual Studio 2019 has already been opened. Click File > New > Project… > Create a new project > Empty Project > Next. The rest as above.
  2. How to Set Up SDL with Visual Studio Picture 6How to Set Up SDL with Visual Studio Picture 6
    Add your Source file to the project.
    1. In "Solution Explorer" window right click the "Source Files" folder (the last one).
    2. Click "Add > "New Item…"
    3. In the "Add New Item - Project-0" window, click "C++ File (.cpp)", the first one. "Name:" Source.cpp is okay.
    4. The "Location" should be C:GLProject-0. If it's not so, click ... to the right of the field and navigate to C: > GL > Project-0. Click "Select folder". Now "Location" is C:GLProject-0.
    5. Click the Add button. The file will open in the main text editor but leave the file blank for now.
Method 4 of 10:

Setting Up SDL2 in Project

  1. How to Set Up SDL with Visual Studio Picture 7How to Set Up SDL with Visual Studio Picture 7
    Configure project's Properties. In "Solution Explorer" right click on the name of your project, that is Project-0, and select "Properties". Leave default settings: Configuration: Active(Debug), and Platform: Active(Win32).
    1. (1) Additional Include Directories.
      1. Open the C/C++ drop-down menu. Click "General" > "Additional Include Directories" > down arrow at the right of the field > "Edit" in the drop-down menu.
        How to Set Up SDL with Visual Studio Picture 8How to Set Up SDL with Visual Studio Picture 8
      2. At the top of the "Additional Include Directories" wizard, click the brown folder icon (the first icon). Click ... next to the text box.
        How to Set Up SDL with Visual Studio Picture 9How to Set Up SDL with Visual Studio Picture 9
      3. In the "Select Directory" window, navigate (by double click): C: > GL > SDL2, and click "include" folder. Make sure you have selected the "include" folder. The "Folder" is "include". Click Select Folder on the "Select Directory" wizard and OK in the "Additional Include Directories" wizard.
    2. (2) Additional Library Directories Open the "Linker" drop-down menu, and click "General". Click "Additional Library Directories" entry > down arrow at the end of the field > "Edit" in the drop-down menu.
      How to Set Up SDL with Visual Studio Picture 10How to Set Up SDL with Visual Studio Picture 10
      1. At the top of the "Additional Library Directories" wizard, click the first icon > click ... next to the text box.
      2. In the "Select Directory" wizard, navigate C: > GL > SDL2, and double click "lib" folder, followed by clicking "x86" folder. The "Folder" is "x86". Click Select folder on "Select Directory" window and OK on "Additional Library Directories" wizard.
    3. (3) Additional Dependencies In the "Linker" drop-down menu, click "Input" > the "Additional Dependencies" entry > down arrow at the right of the field > "Edit" in the drop-down menu.
      How to Set Up SDL with Visual Studio Picture 11How to Set Up SDL with Visual Studio Picture 11
      1. Copy opengl32.lib; SDL2.lib;SDL2main.lib and paste in top-most text box in "Additional Dependencies" wizard > click OK in the "Additional Dependencies" wizard.
    4. (4) SubSystem. In the "Linker" drop-down menu, click "System" > "SubSystem" > down arrow > select "Windows (/SUBSYSTEM:WINDOWS)" from the dropdown menu. Click Apply > OK on the "SDLproject Property Pages" wizard.
      How to Set Up SDL with Visual Studio Picture 12How to Set Up SDL with Visual Studio Picture 12
  2. Copy "SDL2.dll" file and paste into project-folder. In "File Explorer" navigate C: > GL > SDL2 > lib > x86. In "x86" folder click "SDL2.dll" file > right-click > "Copy".
    1. Navigate C: > GL > Project-0. Right-click on empty area in "SDLproject" folder, and select "Paste".
    2. The "SDL2.dll" file is now in project-folder "Project-0" along with "Source.cpp" and other 4 files created by Visual Studio.
      How to Set Up SDL with Visual Studio Picture 13How to Set Up SDL with Visual Studio Picture 13
Method 5 of 10:

Testing your Project

  1. Test your project. Copy following code and paste in Source.cpp code area. Hit Ctrl+F5. If the project builds and a white window with title "An SDL2 window" appears, then the project was set up correctly. You are now ready to program with SDL.
#include "SDL.h" int main(int argc, char* argv[]) { SDL_Init(SDL_INIT_VIDEO); SDL_Window* window = SDL_CreateWindow ("An SDL2 window", // window's title 10, 25, // coordinates on the screen, in pixels, of the window's upper left corner 640, 480, // window's length and height in pixels  SDL_WINDOW_OPENGL); SDL_Delay(3000); // window lasts 3 seconds SDL_DestroyWindow(window); SDL_Quit(); return 0; } 
Method 6 of 10:

Correcting errors if any

  1. In "Error List" if you see error
    1. cannot open file SDL.h, go to Part 4, step 1, Configure project's Properties, sub-step Configure "Additional Include Directories" and follow instructions.
    2. cannot open file SDL2.lib or SDL2main.lib, go to Part 4, step 1, sub-step Configure "Additional Library Directories" and follow instructions. Also to sub-step Configure "Additional Dependencies".
    3. "entry point must be defined" go to Part 4, step 1, sub-step Configure "System" and follow instructions.
  2. In thrown window with X in red circle. If it is said, file SDL2.dll is missing, go to part 4, step 2, Copy "SDL2.dll" file and paste into project-folder and follow instructions.
  3. For other errors. If you cannot correct them, close Visual Studio > delete project folder Project-0 which lives in C:GL > open Visual Studio > repeat set up from Part (Method) 3. Good job.
Method 7 of 10:

Creating Project with SDL Template

  1. Create template. Go to Visual Studio and, while "Project-0" is open, click "Project" > "Export Template...". On "Export template Wizard" check "Project Template", if it's not checked. Click Next >.
    1. On "'Select Template Options'", for "Template name:" type SDL. Click Finish. Template has been created.
      How to Set Up SDL with Visual Studio Picture 14How to Set Up SDL with Visual Studio Picture 14
  2. Create project
    1. Click File > New > Project....
      1. In Create a new project wizard scroll down the list of templates and select SDL > click Next.
      2. In Configure your new project wizard, in "Project name" text field type Project-1. Location should be C:GL. Be sure Place solution and project in the same directory is checked. Click Create.
    2. On Solution Explorer, double click Source Files > double click Source.cpp.
      1. If its code appears, it's okay. If does not, right click on Source.cpp that is below Source Files > click Exclude From Project, and create new Source.cpp file.
      2. If a Wizard appears saying: File 'C:GLProject-1Source.cpp' already exists in the project, go to this path and delete the file Source.cpp. Click OK on the Wizard and then click Add on the Add New Item - Project-1 window. Now on Solution Explore, below Source Files, you have the new Source.cpp file.
    3. Navigate C: > GL > Project-0 > click file "SDL2.dll" > right click > click "Copy".
    4. Navigate C: > GL > Project-1 > click on empty area > right click > click "Paste".
    5. Now a copy of the file "SDL2.dll" is in folder "Project-1" among "Source.cpp" and other 4 files.
    6. In "Solution Explorer" double click Source Files > Source.cpp. Delete code and paste a new one. Hit Ctrl+F5.
  3. How to Set Up SDL with Visual Studio Picture 15How to Set Up SDL with Visual Studio Picture 15
    Use your template. Creating a project with an SDL Template is just like creating an ordinary C++ project, but with one more step:
    1. The file "SDL2.dll" should be copied from a previous SDL project-folder and pasted into new project-folder.
Method 8 of 10:

Create project to target x64 platform

  1. Create empty project as above with name Project-64 and add Source.cpp file.
  2. Project-64's Property Pages main settings. Go to "Solution Explorer" > right click on the name of your project that is Project-64 > select "Properties". In Platform: entry, choose x64 > Click Configuration manager...
    1. In Active solution platform: select x64
    2. In Platform entry, x64 is automatically selected.
    3. Click Close
    4. Tip: Even when in Property Pages wizard, in Platform: entry, x64 is set, click Configuration manager..., and in Active solution platform: select x64.
  3. Additional Include Directories. As above, see Method 4, step 1, sub-step (1).
  4. Additional Library Directories. Open the "Linker" drop-down menu, and click "General". Click "Additional Library Directories" entry > down arrow at the end of the field > "Edit" in the drop-down menu.
    1. At the top of the "Additional Library Directories" wizard, click the first icon > click ... next to the text box.
    2. In the "Select Directory" wizard, navigate C: > GL > SDL2, and double click "lib" folder, followed by clicking "x64" folder. The "Folder" is "x64". Click Select folder on "Select Directory" window and OK on "Additional Library Directories" wizard.
  5. Additional Dependencies. As above, see Method 4, step 1, sub-step (3).
  6. Subsystem. As above, see Method 4, step 1, sub-step (4).
  7. Copy "SDL2.dll" file and paste into Project-64. In Windows's "File Explorer" navigate to
    1. C: > GL > SDL2 > lib > x64. In "x64" folder click "SDL2.dll" file > right-click > "Copy".
    2. C: > GL > Project-2. Right-click on empty area in "Project-64" folder, and select "Paste".
  8. Test your project and correct errors if any. As with targeting platform x86. See Methods 5 and 6. Good job.
    1. Tip: Even if in Property Pages main settings it is Platform: x64, click Configuration manager... and in Active solution platform: select x64.
  9. Create template. As above method 7.
    1. TIP: In every project you create with it, select x64 (next to Debug) in Visual Studio's GUI.
Method 9 of 10:

Compiling Source Code with CMake and Visual Studio

Compiling a library from the source code guarantees that the resulting library is perfectly tailored for your CPU/OS, a luxury pre-compiled binaries don't always provide. It is also important that binaries you get target x64 platform.

  1. How to Set Up SDL with Visual Studio Picture 16How to Set Up SDL with Visual Studio Picture 16
    Highlight what you expect to do. For example see picture above.
  2. Create folder GL. In Windows File Explorer, in directory C:, right click on empty area > select New > Folder > type: GL > hit Enter.
  3. How to Set Up SDL with Visual Studio Picture 17How to Set Up SDL with Visual Studio Picture 17
    Download CMake. Right-click on following address and select Open Link in New Window https://cmake.org/download/. Scroll down the page (see image above). Under "Latest Release (today is 3.16.2)" on the second "Platform" list, find "Windows win64-x64.ZIP" and click the beside entry (today is cmake-3.16.2-win64-x64.zip > in opening wizard select Save file.
    1. Copy and unzip the zip folder.
      1. In downloading window, click folder > right click > in drop-down menu select Copy.
      2. Navigate to directory C: > GL > right click > select Paste.
      3. When copying and unzipping (extracting files) is finished, double click unzip folder cmake-3.16.2-win64-x64 (or latest version) > bin > inside you should see CMake's logo next to file name cmake-gui > double click this file. If wizard "Windows protect your PC" appears, click More information > Run anyway. Now on your screen you have CMake GUI.
      4. Each time you need CMake, navigate to C: > GL > cmake-3.16.2-win64-x64 (or latest version) > bin > double click file cmake-gui (the one with CMake's logo).
  4. Download source code. Right-click on following address and select Open Link in New Window http://libsdl.org/download-2.0.php. Under Source Code select the first option, today is SDL2-2.0.10.zip.
    1. Copy and unzip folder.
      1. In downloading window click zip folder SDL2-2.0.10.zip (or latest version) > right click > select Copy.
      2. Navigate to C: > GL > right click > select Paste.
      3. Now in directory C:GL, you have unzip folder SDL2-2.0.10. Click twice on its name > delete name > type: SDL > hit Enter.
    2. Add the source code. In the CMake GUI first text field, copy C:/GL/SDL and paste.
    3. Add where to build the binaries. In the second text field, copy C:/GL/SDL/build and paste.
    4. Configure and generate. In CMake GUI, click Configure > in wizard Create Directory click Yes > click Finish.
    5. When, in CMake GUI, you read: "Configuring done", click Generate. You should read: "Generating done".
  5. Build your solution.
    1. Navigate to C: > GL > SDL > build. Double click "SDL2.sln", or "SDL2", or "ALL_BUILD.vcxproj". An instance of Visual Studio appears. In the main menu, click "Build" > "Build Solution".
    2. Wait till you read the last line in "Output" window: ========== Build: 5 succeeded, 0 failed, 0 up-to-date, 2 skipped" ==========
      1. TIP: Number of "succeeded" changes in SDL2 versions. Today (28-1-2020) is 5.
  6. Create an empty project. Click 'File > New > Project… > Create a new project > Empty project > Next.
    1. In Project name text box type: pb-0.
    2. Next to Location text box, click ....
    3. Navigate to C: > GL > Click Select a folder. The Location is C:GL.
    4. Check box Place solution and project in the same directory.
    5. Click Create.
  7. Add your source file to the Project. In Solution Explorer wizard, right click the Source Files folder (the last one) > click Add > New Item…
    1. In the Add New Item - pb-0 window, click C++ File (.cpp) (the first one) from the middle of the window. In the Name text box, type Main.cpp.
    2. The Location is C:GLpb-0.
    3. Click the Add button. The file will open in the main text editor but leave it blank for now.
  8. Configure project's Properties. In Solution Explorer wizard, right click Project's name that is pb-0 > select Properties.
    1. (1) pb-0 Property Pages main menu. In Platform entry select x64 > click Configuration Manager....
      1. In Active solution platform: select x64.
      2. In Platform entry, x64 is automatically selected.
      3. Click Close.
    2. (2) Additional Include Directories. Click C/C++ > General > In beside menu select the first one, Additional Include Directories > click the down arrow at the end of the field > click Edit... > first icon > three dots ...
      1. Navigate to C: > GL > SDL > include > click include > click Select a folder > click OK.
    3. (3) Additional Library Directories. Double click Linker > click General > Additional Library Directories > click the down arrow at the end of the field > click Edit... > first icon > three dots ....
      1. Navigate to C: > GL > SDL > build > Debug > click Select a folder > click OK.
    4. (4) Additional Dependencies. In Linker drop-down menu select Input > in beside menu select the first one, Additional Dependencies > click the down arrow at the end of the field > Edit... > copy opengl32.lib; SDL2maind.lib; SDL2d.lib and paste in Additional Dependencies wizard's upper-most text box > click OK.
    5. (5) Set System to SubSystem CONSOLE. In Linker drop-down menu select System > in beside menu select the first one, SubSystem > click the down arrow at the end of the field > select Console (/SUBSYSTEM:CONSOLE). Click Apply and OK.
  9. Copy SDL2d.dll file and paste in pb-0 project. Navigate to C: > GL > SDL > build > Debug, and copy (by right click > Copy) file SDL2d.dll.
    1. Navigate to C: > GL > pb-0, and paste. Now in project folder pb-0 you have DLL file SDL2d.dll among Source.cpp and other 4 files created by Visual Studio.
  10. Test your project and correct errors if any. Follow Methods 5 and 6.
  11. Create template. Follow Method 7. Every time you create project with this template, select x64 in Visual Studio GUI's main menu. Good job.
Method 10 of 10:

Choosing Set Up

  1. In this tutorial you learn 3 was to set up SDL in Project with Visual Studio.
    1. Set up binaries x86 (32 bits). It's the easiest. You should start learning set up from here.
    2. Set up binaries x64 (64 bits). It targets x64 platform. Choose it only when you have specific reason for doing so.
    3. Compile SDL source, and set up in project. Targets x64 too.The most difficult. The best though.
4.4 ★ | 5 Vote