Configuring SFML on Visual Studio manually

2020-02-13 05:50发布

问题:

This Q&A post comes from lots and lots of questions related to sfml library when people try to configure manually their VS projects. Sometimes answers aren't complete or they're too specific.

I would like to compile in a single post how to configure VS to be able to use the SFML library both statically and dynamically.

So:

1. How can I configure my VS project with sfml libraries dynamically, in a general way?

2. How can I configure my VS project with sfml libraries statically, in a general way?

回答1:

First, I recommend carefully follow the SFML tutorial about configuring the library in Visual Studio, if something goes wrong, then check this answer.


I will divide this answer in two groups, how to configure sfml as a dynamic library and how to do it as a static library.

Common steps

  1. Let's create a VS project (I will use VS2013 and SFML 2.5.1, but it's pretty much the same with other versions). Create it as ConsoleApplication and check Empty Project.

  2. Download sfml libraries, latest stable version preferably, selecting your corresponding system (in my case Visual C++ 12 (2013) - 64-bit). Extract this file where your .vcxproj file is. This will create a folder named SFML-X.X.X depending on your version.

  3. Download the external libraries, in my case 64 bits version. Create a folder called extlib inside the library folder and place this external libraries there.

  4. Create a main.cpp file, and paste the example code from SFML tutorials.

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}

SFML As Dynamic Library

  1. Go to Build->Configuration Manager. If you are using a 64 bits library, first you should create a new Solution Platform. Click on Active Solutions Platform->New, select x64 copying from Win32 configuration. I prefer to uncheck Create new project platforms.

  2. Create Debug-Dynamic and Release-Dynamic compilation profiles. With your active solution platform selected, click on Configuration (of the project) and New. You can call it Debug-Dynamic and copy it from Debug configuration (also, uncheck Create new...). Repeat creating a Release-Dynamic configuration.

  3. Open Project Properties->Debugging. Select Debug-Dynamic configuration and modify field Environment with this value PATH=$(ProjectDir)\SFML-2.5.1\bin;%PATH%. This will indicate VS where .dll libraries can be found.

  4. Over C/C++ section, modify Additional include directories field by adding this path $(ProjectDir)\SFML-2.5.1\include. This will indicate VS where .hpp files are located.

  5. On Linker section, modify Additional library directories field by adding this path $(ProjectDir)\SFML-2.5.1\lib. This will indicate VS where .lib files can be found.

  6. Finally, on Linker->Input, modify Additional dependencies field by adding all .lib files needed:

    sfml-audio-d.lib
    sfml-graphics-d.lib
    sfml-network-d.lib
    sfml-system-d.lib
    sfml-window-d.lib

Note -d suffix to indicate debug libraries

  1. Repeat steps 3 to 6 with Release-Dynamic configuration. Note, on step 6, library files doesn't have -d suffix, because they're release libraries

SFML As Static Library

  1. Go to Build->Configuration Manager. Create Debug-Static and Release-Static compilation profiles. With your active solution platform selected, click on Configuration (of the project) and New. You can call it Debug-Static and copy it from Debug configuration (also, uncheck Create new...). Repeat creating a Release-Static configuration.

  2. Open Project Properties and select Debug-Static configuration. Over C/C++ section, modify Additional include directories field by adding this path $(ProjectDir)\SFML-2.5.1\include. This will indicate VS where .hpp files are located.

  3. On C/C++ section->Preprocessor*, modify Preprocessor definitions field by adding SFML_STATIC definition. This will indicate preprocessor that SFML will be statically compiled.

  4. Over Linker section, modify Additional library directories field by adding this paths $(ProjectDir)\SFML-2.5.1\extlib;$(ProjectDir)\SFML-2.5.1\lib;. This will indicate VS where .lib files from external sources and from SFML can be found.

  5. Finally, on Linker->Input section, modify Additional dependencies field with all .lib files needed:

    sfml-audio-s-d.lib
    sfml-graphics-s-d.lib
    sfml-network-s-d.lib
    sfml-system-s-d.lib
    sfml-window-s-d.lib
    flac.lib
    freetype.lib
    ogg.lib
    openal32.lib
    opengl32.lib
    vorbis.lib
    vorbisenc.lib
    vorbisfile.lib
    winmm.lib
    gdi32.lib
    ws2_32.lib

Note -d suffix to indicate debug libraries

Note2 -s suffix to indicate static libraries

  1. Repeat steps 2 to 5 with Release-Static configuration. Note, on step 5, library files doesn't have -d suffix, because they're release libraries, but they'll keep -s suffix.