Home > Net >  include SFML Library in VSCode "SFML/Graphics.hpp no such file or directory" gcc
include SFML Library in VSCode "SFML/Graphics.hpp no such file or directory" gcc

Time:06-12

I've seen other question's relating to this and most if not all that I could find have some round about way of getting SFML to compile and run in VSCode I'm hoping that there is a way to simply append the SFML/include directory to the compilers includes this works with the intelliSense code completion and it correctly knows the location of the SFML Library. intelliSense screenshot.(The extra errors you can see in the screenshot disappear when the file is saved). So ultimately what I would like to know or have help with is getting the SFML library to compile and run without the need for the boilerplate from GitHub that seems to be commonly used thank you.

Main.cpp

#include <SFML/Graphics.hpp>

int main()
{
    float windowHeight = 400;
    float windowWidth = 400;

    sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "Rougelike");

    sf::Texture texture;
    if (!texture.loadFromFile("res/player-sprite.png"))
        return 0;
    sf::Sprite sprite;
    sprite.setTexture(texture);

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

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

This is my c_cpp_properties.json.

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${default}",
                "A:/SFML-2.5.1/include/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "A:/mingw32/bin/g  .exe",
            "cStandard": "c17",
            "cppStandard": "c  17",
            "intelliSenseMode": "windows-gcc-x86",
            "compilerArgs": ["-I A:/SFML-2.5.1/include/**"]
        }
    ],
    "version": 4
}

CodePudding user response:

In VS Code, c_cpp_properties.json specifies parameters used by the editor itself (like compiler path, C standard, include directories for IntelliSense purposes; see documentation here), but doesn't specify build tasks - that is the job of tasks.json. In your .vscode folder, create tasks.json file like this:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile SFML executable",
            "type": "cppbuild",
            "command": "g  ",
            "args": [
                "-o",
                "${workspaceFolder}/main.exe",
                "-IA:/SFML-2.5.1/include/",
                "-LA:/SFML-2.5.1/lib/",
                "${workspaceFolder}/main.cpp",
                "-lsfml-graphics",
                "-lsfml-window",
                "-lsfml-system"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
        }
    ]
}

Here, we created a new task named "Compile SFML executable". This task compiles main.cpp into main.exe, while giving g all the necessary information about SFML: include directories (-I flag), library directories (-L) and main SFML libraries themselves for linker input. Specifically, -l{library_name} links lib{library_name}.a, which is, in our case, import library of stubs referring to dynamic library {library_name}-2.dll (this name is coded inside import library), which can be found in A:/SFML-2.5.1/include/bin/ folder (assuming you installed MinGW version of SFML libraries, which you should have, since you use MinGW G compiler). {library_name} here refers to sfml-window, sfml-system and sfml-graphics, which, judging by your code, are the only ones you need right now. Note that dynamic libraries have to be copied to executable folder (or wherever else system dynamic loader will be able to find them) before running.

You can run the task by Ctrl-Shift-P -> Tasks: Run Task -> Compile SFML executable, and after that run resulting executable outside of VS Code. Alternatively, you can use F5 or Ctrl-F5 within VS Code to run executable with or without debugging respectively, where the executable will be built prior to run according to a default launch configuration's preLaunchTask, which by default is set to our task (because of "isDefault": true).

Also note that if you want to do debug builds/runs, you should link to {library_name}-d instead of {library_name} (and copy {library_name}-d-2.dll into executable folder to run) instead. Creating separate task (like "Compile SFML executable Debug") with appropriate -l{library_name}-d flags can be useful for this. You can then choose which build task to perform before run by removing "isDefault": true from both configurations and using Ctrl-Shift-P -> C/C : {Debug / Run} C/C File and choosing launch configuration with appropriate preLaunchTask from the list.

  • Related