Gigi Labs

Please follow Gigi Labs for the latest articles.

Monday, February 10, 2014

SDL2: Setting up SDL2 in Visual Studio (2013 or any other)

Hi ladies and gents! :)

[Update 2015-11-14: This article is out of date. Check out the latest version at Gigi Labs.]

A few months ago, in my article "SDL2: Setting up SDL2 in Visual Studio 2010", I wrote how you can get SDL2 working with Visual Studio by copying the headers and libraries into the folders where Visual Studio expects to find them by default.

The problem with this approach is that it might work for one version of Visual Studio, but you'll have to repeat a similar setup when you upgrade to a different version of Visual Studio. In fact, in this article we're going to use Microsoft Visual Studio Express 2013 for Windows Desktop (VS2013 for short). The steps are very similar TwinklebearDev's "Setting up SDL in Visual Studio".

First, head to the SDL2 downloads page and download the Development Libraries for Visual C++. At the time of writing this article, it's listed as SDL2-devel-2.0.1-VC.zip. Extract the contents of this file to a convenient location - for this tutorial it's going to be in C:\sdl2. This folder should directly contain the include and lib folders, so leave out any intermediate folders (at the time of writing this article, there is an SDL2-2.0.1 folder containing them in the package, so just leave that out).

Now you can fire up VS Express 2013 for Desktop (that's how VS2013 is listed if you search for it in the Windows 7 Start menu) and hit the "New Project..." link. Under "Visual C++", select "Empty Project":


In Solution Explorer, right click on the project and select "Properties". Under C/C++, then General, set the "Additional Include Directories" to "C:\sdl2\include" (or wherever you placed your SDL2 include folder):


Under Linker, then General, set the "Additional Library Directories" to "C:\sdl2\lib\x86" (adapt this path depending on where you extracted your SDL2 files):


Still under Linker, but this time going into Input, replace the value of "Additional Dependencies" with "SDL2.lib; SDL2main.lib":


And still under Linker, but now selecting System under it, set "SubSystem" to "Windows (/SUBSYSTEM:Windows)":


That's more or less the configuration we need. This is to compile an SDL2 game as an x86 executable, which should be fine if you're just starting out (since it will work on both 32-bit and 64-bit platforms). If you want to compile as x64 instead, the steps above are the same, except that you'll need to use the x64 libraries instead.

Now that the configuration is done, let's add some code and see that it works correctly. In Solution Explorer, right click on the "Source Files" filter, and then select "Add" -> "New Item...":


At this point, add a C++ file, and name it main.cpp:


Type in some code. The intellisense in VS2013 works nicely:


This code should get you going just fine:

#include <SDL.h>

int main(int argc, char ** argv)
{
 SDL_Init(SDL_INIT_VIDEO);

 // game code eventually goes here

 SDL_Quit();

 return 0;
}

If you press Ctrl+Shift+B to compile the project, it compiles just fine. But if you press F5 to run it, the following error appears:


Don't panic! We've been through this before. All you need to do is retrieve your SDL2.dll file from C:\sdl2\lib\x86 (adapt according to where you placed your SDL2 files) and copy it into your project's Debug folder, where the executable is being generated.

If you run the program now, nothing happens, and that means it worked fine. You can proceed to build your game from here! :)

In this article, we have revisited the configuration necessary to set up an SDL2 project, this time with VS2013 and by configuring settings in Visual Studio as opposed to throwing files into the Windows SDK directories. Here's a summary of the things you have to configure:

  1. C/C++ -> General: Set "Additional Include Directories" to the SDL2 include folder.
  2. Linker -> General: Set "Additional Library Directories" to the SDL2 libraries folder depending on the platform you are targeting.
  3. Linker -> Input: Set "Additional Dependencies" to "SDL2.lib; SDL2main.lib".
  4. Linker -> System: Set "SubSystem" to "Windows (/SUBSYSTEM:Windows)".
  5. Remember to put SDL2.dll in the same directory as the executable!

Enjoy your SDL2 coding! :)

9 comments:

  1. holy crap ty so much it finally works my god for every tutorial i keep running into brick walls with linker errors finally did what i wanted been at this for a few days now so freaken happy

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Everything works but I receive this:

    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\gdi32.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\imm32.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\msvcrt.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\ole32.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\oleaut32.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\shell32.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\user32.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\version.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\winmm.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\msctf.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\combase.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\rpcrt4.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\sechost.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\shlwapi.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\winmmbase.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\sspicli.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\cfgmgr32.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\devobj.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\cryptbase.dll' completato. Impossibile trovare o aprire il file PDB.
    'ProvaSDL.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\bcryptprimitives.dll' completato. Impossibile trovare o aprire il file PDB.

    In Project>Properties>Linker>Input I have this:
    "SDL2.lib; SDL2main.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)"

    I have added the part described in your tutorial and I've tryied writing "SDL2.lib; SDL2main.lib;" only... with and without the last ";" cause you haven't typed it!

    Thank you in advance!

    ReplyDelete
    Replies
    1. That's strange, but it seems you can safely ignore it. See this for details:
      http://stackoverflow.com/a/15938020/983064

      Delete
  4. i'm finding the error of this type please tell me the solution
    error LNK2019: unresolved external symbol _SDL_Init referenced in function _main

    ReplyDelete
    Replies
    1. Have you followed all the steps in this article? Particularly the linker ones?

      Delete
    2. I'm running into the same problem. The compiler returns three errors, all unresolved external symbols. These are _SDL_Init and _SDL_Quit referenced in _SDL_main, and _WinMain@16 referenced in __tmainCRTStartup.

      Delete
    3. Hi, i had the same problem, I used x64 library with "Debug win32", try x86 library.

      Delete
  5. Thanks for sharing this! All of the other instructions I tried failed to make SDL work on VS2013 until I tried yours!

    ReplyDelete

Note: Only a member of this blog may post a comment.