Search code examples
c++visual-studio-2012sdlsdl-ttf

Trying to install SDL_ttf in Visual Studio Express 2012 for Windows Desktop


I am trying to install SDL_ttf in Visual Studio Express 2012 for Windows Desktop and I have a small sample program that compiles fine, but when I run it, I get an error popup that says "The application was unable to start correctly (0xc000007b). Click OK to close the application".

I originally got basic SDL to work, then I attempted to add SDL font. I have done the following, but I'm still having trouble, if someone could hep me out, it would be much appreciated...

1) I copied all the .lib folders into the Visual Studio lib folder (C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\lib) (which are SDL.lib, SDLMain.lib and SDL_ttf.lib)

2) I added my include directories for SDL and SDL_ttf in the project properties (under Project >> Properties >> Configuration Properties >> VC++ Directories >> Include Directories) ... Those are ("...\SDL Main Libraries\SDL-1.2.15\include") and ("...\SDL Font Libraries\SDL_ttf-2.0.11\include")

3) I added my additional dependencies for SDL and SDL_ttf (under Project >> Properties >> Configuration Properties >> Linker >> Input >> Aditional Dependencies, where I put: SDL.lib SDLMain.lib SDL_ttf.lib inline - it looks like this: SDL.lib;SDLMain.lib;SDL_ttf.lib;%(AdditionalDependencies)

4) I have put the following dll files in the same folder as my .exe file (which is Visual Studio 2012\Projects\ConsoleApplication2\Debug), those dll files are: SDL_image.dll libfreetype-6.dll SDL_ttf.dll zlib1.dll SDL.dll

And this is my small sample program source code:

#include <iostream>
#include <SDL.h>
#include <SDL_ttf.h>

using namespace std;

int main(int argc, char** argv){
    int retval = 0;

    int sdlState = -1;
    if((sdlState = SDL_Init(SDL_INIT_EVERYTHING)) == -1){
        cerr << "SDL failed to initialize";
        retval = 1;
    }

    SDL_Surface* screen = nullptr;
    if(retval == 0){
        if(nullptr == (screen =
            SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_ASYNCBLIT)))
        {
            cerr << "Screen failed to be created";
            retval = 1;
        }
    }

    int ttfState = -1;
    if(retval == 0){
        if((ttfState = TTF_Init()) == -1){
            cerr << "True Type Font failed to initialize";
            retval = 1;
        }
    }

    if(retval == 0){
        //TTF_Font* font = TTF_OpenFont("air.ttf", 32);
        SDL_Color txtColor = {0, 0, 0};
        //SDL_Surface* text = TTF_RenderText_Solid(font, "Hello World",
            //txtColor);
        while(1){
            SDL_FillRect(screen, NULL,
                SDL_MapRGB(screen->format, 255, 255, 150));
            //SDL_BlitSurface(text, NULL, screen, NULL);
            SDL_Flip(screen);
        }
    }

    if(ttfState != -1) TTF_Quit();
    if(sdlState != -1) SDL_Quit();
    return retval;
}

Solution

  • This question could probably be renamed "Trying to install a 3rd party library in Visual Studio Express 2012 for Windows Desktop". While your steps work, they are somewhat not optimal, especially if you plan to share your project and work with others on it!

    Basically, these kind of libraries come with 3 main file types, headers, libraries and binaries. Here's a few hints on how to improve your project:

    headers - these files define the interface you have with the library. Usually, to let Visual Studio know their location, the preferred method is to go in your Project Settings > Configuration Properties > C/C++ > General > Additional Include Directories. If you want to share your project with someone (probably through source control like Perforce, Git or SVN) you should use Relative Paths instead of Absolute Paths. What I mean by that is that if you target directly a file like C:\Projects\SDL\... you end up putting some requirements about where to store your project for every member on the team, as if you use something like ..\External\SDL\... wherever the other team members store your solution, it should work, as long as they have the right libraries in the right relative folder.

    libraries - there is two parts for the libraries. First, you need to tell the linker where to look for the required libraries. Do NOT copy your libs directly in your VC\libs folder, instead use Project Settings > Configuration Properties > Linker > General > Additional Library Directories. The same rules about relative folders apply here. Then, you need to tell the linker which libraries to use, this is done through Project Settings > Configuration Properties > Linker > Input > Additional Dependencies where you list all the .lib files you want, such as SDL.lib, SDLmain.lib, SDL_ttf.lib, etc...

    binaries - the binaries here will be the .dll files you need. A simple option is to simply copy them manually to your Output folder (remember you can have multiple Configuration, not only Debug). Otherwise, you could automate the process by having some Post-Build steps that copy them automatically for you.

    For all of these settings, consider that the Project Properties Window is by default targeting a single configuration! To have your changes applied to every configuration, in the dropdown on the top that probably says Active(Debug), select All Configurations, then change the settings. They will be applied to every configuration you have in your project.

    Edit: Also, for sharing projects, depending on the mentality behind the project, you might want to include your external libraries on your source control. I know the linux people will totally object to that, but that's something that I've seen very often. It makes sure that everybody is using the library version that corresponds with the code (for example, if you upgrade to a newer version of the library and the interface changed, if you only change the code on source control, you will have to warn your other team members to manually upgrade to the new library, as if it was on your source control, a sync would make them up to date with both the project's code and the external libraries).