[SDL2 – Part 1b] Setting up Visual Studio for SDL2

Setup Visual Studio for SDL2


Finally, I’ve gotten around to making a quick guide for setting up Visual Studio for SDL2. This guide also includes a fix that makes it possible to use SDL2 with Visual Studio 2015

In order to use SDL2 on Windows, you have to set up your IDE to use it. Here’s the guide for how to do that using Visual Studio. The steps are generally the same for all versions of Visual Studio, but there is an issue with Visual Studio 2015

Visual Studio 2015


They changed a lot in the 2015 versjon of Visual Studio. This change means that you get a linker error when you try to build an SDL2 project.

It took me a little trial and error to fix this, but I ended up building the SDLmain from source. You can find it here.

1. Getting the libs

You can find the files you need here. For VisualStudio you need to download :

SDL2-devel-2.x.x-VC.zip (Visual C++ 32/64-bit)

This includes both the .lib and the .h files.

Or, as mentioned above, if you’re using Visual Studio 2015, you need a .lib file build with Visual Studio 2015. You can either do this yourself, or download the ones I compiled.

Placing the includes/libs


Now take all the .h files in include and move them into a folder named SDL2. You can put this folder anywhere you want as long as the folder containing all the .h files is called SDL2. The reason for this is that we use #include <SDL2/SDL.h>

Do the same for the .lib files. The name of the directory you put them in is irrelevant in this case, just put them somewhere you remember. ( You might have to put other .libs in here at a later point in time )

2 Setting up libs


Start up Visual Studio, create a new project and add / write a .cpp ( for instance you can use the main.cpp in the first part of the tutorial. )

Now we need to set up VisualStudio so it knows where to find the header files we placed in the step above

Right click on the project and click “Properties”


VS Install 1

Select C/C++, select “Additional include directories” and click “Edit”


VS Install 2

Click “New Line”, then navigate the folder containg the SDL2 folder and click “Select Folder”


VS Install 3

You should now see something like this :

VS Install 4

Click OK. Now we’re done with the header files, time for the lib files.

Under “Linker”, select “Additional Library Directories


VS Install 5

Do the same thing you did for the header files, but this time navigate to the folder conataining the .lib files.

Navigate to “Input” and enter “SDL2main.lib;SDL2.lib;” in front of the others


VS Install 6

3 Copying .dll files


The .dll files are needed to run SDL2 applications. When it comes to placing them you have two options :

In the project directory

This is the same folder as your .exe file. This means you have to copy them every time you create a new project, which can be a little annoying and easy to forget

In your Windows system directories

When Windows looks for dll files, it’ll look in a few standard directories in addition to the directory the .exe file is in. Putting it in one of these means the dll will always be there, and you don’t have to worry about copying.

The directories are as follows :

  • In x86 this directory is C:/Windows/system32/
  • In x64 this directory is C:/Windows/SysWOW64/ though you might have to place them in System32/ as well.

4 Setting correct subsytem


You’ll probably also have to set the correct subsystem. Go to Linker -> System and set SubSytem to Console(/SUBSYSTEM:CONSOLE)

VS Install 7

Adding other libs


Now that we have this set up, we can add other SDL2 libs like SDL2_image, SDL2_ttf, etc.. All you have to do, is to download the Visual Studio libs like before and copy the header files and lib files to the same folders as above. You also need to add the name of the new .lib file to “Input” under “Linker” And finally you need to copy the new dlls as mentioned above.

SDL2_image

You can find the libs here ( download the one with VC in it. ) Add

SDL2_image.lib

to Linker / Input

SDL2_ttf

You can find the libs here ( download the one with VC in it. ) Add

SDL2_ttf.lib

to Linker / Input

SDL2_net

You can find the libs here ( download the one with VC in it. ) Add

SDL2_net.lib

to Linker / Input


Feel free to comment if you have anything to say or ask questions if anything is unclear. I always appreciate getting comments.

You can also email me : olevegard@headerphile.com
Share

18 thoughts on “[SDL2 – Part 1b] Setting up Visual Studio for SDL2”

  1. Thanks for your helpful blog. I recently got SDL2 working on Visual Studio Express 2013, and I have a question that is slightly off-topic, but here goes.

    With SDL1.2 I was able to use fprintf(stdout… and fprintf(stderr… , but apparently you can’t do that with SDL2. I don’t want to use the console, I want to use the WINDOWS subsystem and fprintf. In some cases I need to print a lot of pixel data and then to analyze it, usually in Excel. The console would not be my choice for that. I have rigged up my own file-printing mechanism, but I’m in the habit of using fprintf and would like to go back to it if possible. An online search tells me that it’s not possible with SDL2. Are you aware of a way to do it? Thanks again.

    1. Well I can’t say I have ever used fprintf() because I try to do everything “the C++ way” : P But if what you want is file output, you can do this using fstream If you need to print the pixel data, you can access it from SDL2 and print it using fstream. fstream is very fast, so printing a lot of data shouldn’t be an issue.

      As for Windows subsystem, I don’t really know anything. I stick to Linux, it’s so much better for programming ; )

      Sorry I can’t really help you any better. If you want a better answer, you could try stackoverflow.com

      1. You beat me to it on my second comment. Thanks for the reply; I will follow your suggestions. And thanks again for the blog, it’s helpful.

  2. Thank you for your tutorial first.
    But, I have a little problem.
    I use the visual studio 2015 with windows7 64bit OS.
    When I compiled the code, I got the error 0xc00007b. I think your visual studio lib files are made for 32bit computer.
    Is that right?

    1. Hello

      I don’t have a Winodws computer at home anymore, but I think 0xc00007b means that there is an issue with your dll files.

      Whether you are running 32bit or 64bit doesn’t really matter. The important thing is that the dll files are compatible with each other. If you have configured your project for 64, try configuring it for 32bit.

      You can also try debugging your exe with depends ( http://www.dependencywalker.com/ ). It’s a old tools for checking executeable files, but its very helpful for resolving issues like this.

      Hope that helps. If you still have issues, just reply or post in the new forums

    2. For anyone that ends up here via Google, if you download the files from the SDL website, you can get rid of this error.

  3. Thank you for taking the time to make this post, I followed the instructions and it still won’t work for me, I haven’t had problems when setting it up on CodeBlocks but Visual Studios 2015 has just been 1 problem after another. Anyway I get the error ‘C1083 Cannot open include file: ‘SDL.h’ no such file or directory’

    I’ve been looking all over on how to set up SDL2 on Visual Studios 2015 and nothing seems to be working for me.

    1. Okay scratch that, I am stupid, I noticed when I had the project properties open the configuration on the top left wasn’t set on the active one. So yeah I fixed that and now everything is working properly. Anyway Thank you very much for posting this it helped me a lot.

  4. Thanks for the guide. I’m trying to get SDL_ttf working with VS2015 but I’ve been running into errors all over the place and I think its down being built for 2013. Do you know how I would compile from source?

    1. Yeah, there are some runtime issues with VS 2015. You need can try downloading the source from here : https://www.libsdl.org/projects/SDL_ttf/

      Select SDL2_ttf-2.0.12.zip and extract the contents. Inside it, open the folder named VisualC. Inside it you’ll find solution files you can open in VS 2015. Try building these and use the output files when compiling SDL. Please tell me if you run into some other issues ( which is very likely 😛 )

      Hope this helps. I really hope t hey will publish files that work for VS 2015 as well. Hopefully they will soon.

  5. When copying the DLL to your working directory, you can automate this adding a Post-Build event.

    Go to Project Properties > Configuration Properties > Build Events > Post-Build Event
    Then add the following:

    copy “C:SDL2-2.0.4libx86SDL2.dll” “$(OutDir)SDL2.dll”

    (note you could have to change the path to library for your system)

    If you are using extension libraries like SDL2_image, you could add something like:

    copy “C:SDL2-2.0.4libx86SDL2_image.dll” “$(OutDir)SDL2_image.dll”
    copy “C:SDL2-2.0.4libx86libpng16-16.dll” “$(OutDir)libpng16-16.dll”
    copy “C:SDL2-2.0.4libx86zlib1.dll” “$(OutDir)zlib1.dll”

    The only problem is, it will copy the files everytime you compile the project, normally it shouldn’t be an issue. The good thing is you always have the files where you need without messing with the user Window System (you can copy dll there but only for developing in your computer, because you could overwrite some user’s dlls if you don’t pay attention).

    1. Hei 🙂

      Automated building is nice, and that script is useful. But it will fail when the path to the dlls are different or when the files have changed. Maybe I’ll add a Python script that does all of the setup one day.

    2. Another option:
      1, set up a separate, empty project within your solution, with just the post build step, and
      2, making sure that your main project is set as startup project.

      This way building the solution will copy the files, but building just your project won’t.:)
      Hope this is useful.

  6. Super helpful Thank you!

    Another way to set things up in Visual Studio 2015 (and earlier versions I think?) is to include the SDL.vcxproj and SDLmain.vcxproj project files in your solution directly. (Right click the solution, add existing project). The files live in the VisualC directory of the SDL source.

    Now under your project right click references and add a reference to the two SDL projects. This takes care of all of the .lib stuff for you so you can skip that part.

    Visual Studio will copy the .dll files for you too but the SDL projects don’t use the default settings so you’ll have to change some stuff. Either update your project or update the SDL projects “Output Directory” so that they’re all dumping to the same place. Right click the project, Properties, General, Output Directory. (I just copy pasted the value from the SDL project to mine)

    Finally you’ll still have to point VS to the SDL “include” directory with all the .h files (more or less) as described in the post.

    Now you should be set. Every time you build Visual Studio will make sure you’re referencing the right stuff and copy the relevant files to the right places regardless of if you’re doing a debug/release/x86/x64 build.

    Anyway hope that helps someone. It’s way cleaner imo. No (manual (or script)) copy pasting files around (woohoo!) and you’ll be able to look at/debug into the SDL source if you’re into that kind of thing…

    PS. I couldn’t get SDL to work till I found this post so thanks again!

Leave a Reply

Your email address will not be published. Required fields are marked *