Monday, July 9, 2012

Setting up SDL in Visual Studio

Deprecation Notice:

I've been updating the lessons now that SDL2 is officially released, please visit my new site on Github that has updated lessons and improved code.

Now that we've got the libraries setup we'll want to link up the them up with our project, so that we can begin using the library, start up Visual Studio and create a new empty C++ project.

To make our project recognize the headers and libraries for SDL we'll have to edit some simple configuration options about our include directories and linker settings.

Right click project name and bring up the properties menu. Under the C/C++ listing select General and click the Additional Include Directories item at the top.

Click the down arrow button at the end of the text entry box, and select Edit.

Click on the New Folder icon to add an entry to the list and click the Browse Button (the three dots) at the right side of the text box. Navigate to where you put your SDL folder, and within it select the folder titled 'include'. Hit Select Folder, Ok and Apply to save your selection.

Next we'll want to change the Runtime Library, in the C/C++ menu select Code Generation and find the Runtime Library entry. Select it and change it to Multi-Threaded DLL.

To link up the libraries select the Linker listing beneath C/C++, select the General tab and click on the Additional Library Directories entry, located just about in the middle of the window.

As before, click the down arrow button at the right side of the text box, and select Edit

Again select the New Folder button and click on the Browse button (three dots) at the end of the entry box. Navigate to your SDL folder and descend into the folder titled 'lib'. Here you will see two more directories, x86 for 32bit and x64 for 64bit. While you may choose either, keep in mind that by default VC++ is configured to compile to 32bit and you will have to change some other settings accordingly. Select the folder you want to use, I'll be using x86 for this tutorial, hit Select, Ok, and Apply to save your changes.

Next we must specify which libraries we want to link against, so under the Linker menu, select the Input menu and click on the Additional Dependencies text box, selecting as before the down arrow and Edit to bring up the menu.

To let VC++ know which libraries we want to it to use, you will want to enter:
SDL.lib; SDLmain.lib;

Hit Ok and Apply to save the configuration. 

The final step is to change our SubSystem target to Windows. To do this, select the System menu under Linker and change the entry in the SubSystem to Windows.

Hit Apply to save the configuration and Ok to close the properties window.

Now let's test if we've configured everything correctly. Add a new C++ source file to the project and enter

This simple code will initialize SDL and then quit, nothing visible will occur. Try compiling the project, if you get any errors, make sure you didn't skip a step and selected the correct folders. Before you can run this code you will need to put the Dynamic-Linked Library SDL.dll, found in the bin folder of your SDL directory, into the same folder as the executable. If you don't the program will fail to run and will pop up an error that it couldn't find SDL.

As a last extra to save the trouble of having to do this for every SDL project you create, you can export this project as a template. Select the File menu and click Export Template and export it as a Project Template. You can name and set description as you like. Now when creating a new SDL project you can simply select your template, and as long as you haven't moved your SDL folder around everything will already be configured!

End of Lesson 0

If you're having trouble setting up SDL please leave a comment below.

I'll see you again soon in Lesson 1: Hello World


  1. How did you get your Visual Studio to look so nice, like, the icons. Is that 2011 or 2012?

    1. Ah I'm using Visual Studio 2012 RC, they definitely prettied it up a good deal heh

  2. Hey Twinklebear,

    When I downloaded SDL via Mercury, there was no lib folder in the downloaded directory. I was able to compile after I firstly deinstalled my c++ redistributable and then reinstallied the directx SDK, but when I wanted to set up my sdl 2.0m there was no lib folder available.

    Do I simply have to create a lib folder with the libs I created while compiling? Are they working for x86 as well as x64?

    1. If I recall correctly, when you build the library the libs are put in a build output folder, then you copy them from there.

      I just built SDL to test, and it looks like they're put under VisualC/SDL/Win32/Debug/ or Release depending on the configuration you built. SDLmain will be put under the same path but SDLmain instead of SDL. All the object files are stuck in there too, so you may have to dig a bit to find the .lib and .dll files you built.

      So copy the libs from there into your lib folder, and the dll files into the same folder as your exe. It's good to hang onto the dlls in their own folder so you don't lose'em on accident.

      Be sure to identify the Debug libs with something like a "-d" if you plan to use them.

    2. I don't get it still, shouldn't there be a 'lib' folder like SDL 1.2 containing all the *.lib files?

    3. The .libs are under VisualC/SDL/Win32/Debug/ or Release depending on the configuration you built. You should build both and use debug for debug builds and release for release builds, or if you only want to build one then just build the release. SDLmain is under VisualC/SDLmain/Win32/Debug/ or Release depending on the configuration built.

      So you want to copy them from there and put them into a lib folder. Since you only need the headers (under include/ I believe) and the libs which you've just built, you can stick those in some SDL2RC folder and delete everything else.

    4. Thanks sir, because of my lack of Visual Studio understanding I didn't figure out I had to actually open the VisualC2012 project file and build it to generate the corresponding *.lib files.

      I built SDLmain.lib, but the SDL.lib isn't being generated and Visual Studio throws three errors:
      1>..\..\src\audio\xaudio2\SDL_xaudio2.c(31): fatal error C1083: Cannot open include file: 'dxsdkver.h': No such file or directory
      1>d:\sdl\src\stdlib\sdl_string.c(34): warning C4717: 'SDL_atoi' : recursive on all control paths, function will cause runtime stack overflow
      1>d:\sdl\src\stdlib\sdl_string.c(35): warning C4717: 'SDL_atof' : recursive on all control paths, function will cause runtime stack overflow

      I got the latest files from Mercurial, maybe a bug has been introduced or something, any tips? Thank you so much for your help! :)

    5. Ah no bug, but to build SDL on Windows you need the DirectX SDK installed, that's what "fatal error C1083: Cannot open include file: 'dxsdkver.h': No such file or directory" is telling you, it can't find DirectX SDK. If you're on Windows 8 the SDK is included, but maybe it's not finding it, so you should look up how to link and include it. If you're on 7 or lower you'll need to download and install the SDK from here:

    6. I can't seem to edit my comment for some reason, but I'm not sure about the recursion warnings. They are just warnings, so I guess just don't use those functions, you don't need to anyways since the STL has functions for those operations.

    7. This comment has been removed by the author.

    8. Windows 7 x86 here, I installed DirectX SDK and now everything compiled correctly!
      I'm follow the tutorials now, thanks for your patience. :)

  3. Gah, I got a SDK.dll file not found error, and then I realized I was using x86 files instead of x64.

    Thanks for the tutorial, I should have read more carefully!

  4. I just got it all running (Visual Studio 2012 with SDL) thanks to you.
    I could give you a big, wet kiss right now! Which it'd be coming from a dude with a beard, but hey you made me happy bro!

  5. Hi,
    I follow this tutorial. But when I run the little code it gives an error about SDL.dll. I put the file in system32 directory, and the problem that occur is "cannot open the file" (like it can not be used for windows).
    Do you have any idea what can be wrong?

    Thanks for the attention.
    Kind Regards,

    1. Yep: SDL.dll should be put in the same directory as the executable, not the system directory (I think this is the same for any dynamic linked library too, for future reference).

  6. Nice job. thumb up and peacefull from Indonesia...

  7. The official SDL2 Release Candidate libraries have names that start with SDL2 rather than SDL. This screwed me up for a while because I was blindly following the tutorial and not paying close attention to what I was actually trying to link.

    1. Ah yea I've got to find some time to update the instructions now that the RC is out.

  8. Hey, I've tried following this tutorial word for word on 2 different systems, both Win7 x64. On one, I get a 'Cannot open rc.exe' error - On the other, everything compiles, but then fails and says 'The program '[2044] Test.exe' has exited with code -1073741701 (0xc000007b)'.

    Thoughts? Thanks!

    1. Ah I'm not really sure, it could be a configuration error or something? I've never heard of rc.exe or this error, sorry.

  9. Thanks for your tutorial. But I seems not to work with my computer (Windows 7 64 Bit / VS 2012 / SDL1.2)

    "SDL_Setup.exe" (Win32): "C:\Users\Martin\Documents\Visual Studio 2012\Projects\SDL_Setup\Debug\SDL_Setup.exe" geladen. Symbole wurden geladen.
    "SDL_Setup.exe" (Win32): "C:\Windows\SysWOW64\ntdll.dll" geladen. PDB-Datei wurde nicht gefunden oder konnte nicht geöffnet werden.
    "SDL_Setup.exe" (Win32): "C:\Windows\SysWOW64\kernel32.dll" geladen. PDB-Datei wurde nicht gefunden oder konnte nicht geöffnet werden.
    "SDL_Setup.exe" (Win32): "C:\Windows\SysWOW64\KernelBase.dll" geladen. PDB-Datei wurde nicht gefunden oder konnte nicht geöffnet werden.
    "SDL_Setup.exe" (Win32): "C:\Windows\SysWOW64\SDL.dll" geladen. Das Modul wurde ohne Symbole erstellt.
    "SDL_Setup.exe" (Win32): "C:\Windows\SysWOW64\advapi32.dll" geladen. PDB-Datei wurde nicht gefunden oder konnte nicht geöffnet werden.
    "SDL_Setup.exe" (Win32): "C:\Windows\SysWOW64\msvcrt.dll" geladen. PDB-Datei wurde nicht gefunden oder konnte nicht geöffnet werden.
    "SDL_Setup.exe" (Win32): "C:\Windows\SysWOW64\sechost.dll" geladen. PDB-Datei wurde nicht gefunden oder konnte nicht geöffnet werden.
    "SDL_Setup.exe" (Win32): "C:\Windows\SysWOW64\rpcrt4.dll" geladen. PDB-Datei wurde nicht gefunden oder konnte nicht geöffnet werden.

    and many more...

    1. Hmm, I don't speak German but if you post it in English I may be able to give some tips. Try running through the updated setup guide first though, that may fix the issue:

    2. It's trying to download debugging symbols for the associated .dll file. It's failing to find the debug symbols. It's just a warning and unless you want to debug the MS windows dll files then don't worry. Yeah, I know it looks ugly but it nothing to worry about.

  10. Hi Twinklebear! When I try to run any SDL code, I get this error: "Unable to successfully start the application (0xc000007b)". I tried to run a simple c++ code and it works, so what am I doing wrong?
    I use VC++ 2013

    1. Already solved, I had to put SDL2.dd in the Debug directory of my project instead of System32..

  11. Error Error 1 error LNK1104: cannot open file 'SDL.lib' C:\Users\Alexander\Documents\Visual Studio 2013\Projects\SDL_Tutorial\SDL_Tutorial\LINK SDL_Tutorial
    :( Please help

    1. It should be SDL2.lib to find the SDL2 libraries, and double check the library path is set properly. Also check out the updated versions of the lessons:

      They've got a lot of improvements on them.

  12. Hey, it would be super helpful if you added the information in the comments about the updated version ( at the beginning of the article. Thanks!

    1. Oh you're right! I forgot to add the notices on these pages.