How do i link the SFML libraries in Visual Studio

2020-02-01 14:48发布

问题:

I've been trying for hours and I can't seem to do it I've downloaded extensions and asked for help around but everything is just confusing me at this point. I want to include the SFML libs in my project and I'm trying to use the the Visual Studio Code editor for it but it just won't comply for some reason.

A picture of what it currently looks like. http://imgur.com/qJPlJua

I've been trying this for hours yesterday also but it just doesn't want to work.

回答1:

I searched and I have finded.

In the tasks.json file, define two tasks :

"tasks": [
    { 
        "taskName": "Compilation",
        "isBuildCommand": true,
        "args": ["-c", "${workspaceRoot}\\main.cpp", "-IC:\\SFML-2.4.0\\include"]
    },
    { 
        "taskName": "Liaison du fichier compilé aux bibliothèques SFML",
        "args": ["${workspaceRoot}\\main.o", "-o", "sfml-app.exe", "-LC:\\SFML-2.4.0\\lib", "-lsfml-graphics", "-lsfml-window", "-lsfml-system"]
    }
],

and add "suppressTaskName": true,

So it's like on Linux.

You compile with CTRL + SHIFT + B. For create the .exe file : CTRL+SHIFT+P --> then "run task" then click then click on the "Liaison du fichier compilé aux bibliothèques SFML" task.

the entire file is as (for me):

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "g++",
"isShellCommand": true,
"suppressTaskName": true,
"tasks": [
    { 
        "taskName": "Compilation",
        "isBuildCommand": true,
        "args": ["-c", "${workspaceRoot}\\main.cpp", "-IC:\\SFML-2.4.0\\include"]
    },
    { 
        "taskName": "Liaison du fichier compilé aux bibliothèques SFML",
        "args": ["${workspaceRoot}\\main.o", "-o", "sfml-app.exe", "-LC:\\SFML-2.4.0\\lib", "-lsfml-graphics", "-lsfml-window", "-lsfml-system"]
    }
],
"showOutput": "always"

}

Cordially.



回答2:

I know this question is about two years old, but after fiddling with my own tasks to solve this problem, and came up with something. This shouldn't be the best way to do it, but this should be good for anyone that finds this answer in the future.


{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile",
            "type": "shell",
            "group": "build",
            "command": "g++",
            "args": [
                "${file}",
                "-o",
                "${fileBasenameNoExtension}.exe",
                "-IC:\\SFML-2.5.1\\include",
                "-LC:\\SFML-2.5.1\\lib",
                "-lsfml-graphics",
                "-lsfml-window",
                "-lsfml-system",
            ],
            "problemMatcher": [
                "$gcc"
            ]
        }
    ],
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared"
        //"showReuseMessage": true
    }
}

This should work the same as the above answer. Hit CTRL+SHIFT+B to bring up the Task prompt, or look up Run task in the Command Palette (CTRL+SHIFT+P). Remember to have the .dlls of each library used in the root of the project.

Hope this helps.



回答3:

I know the topic is a couple years old now but since I was searching for a way to link the sfml lib in vs code and I first ended up here, I thought I would share this git repo I found, which works pretty well for me so far:

https://github.com/andrew-r-king/sfml-vscode-boilerplate

I'm not using SFML 2.5.1 though, so I had to bring a small change in the c_cpp_properties.json file (I am on Ubuntu 18.04 and installed sfml through package manager)

here my c_cpp_properties.json file:

{
    "configurations": [
        {
            "name": "Linux",
            "intelliSenseMode": "gcc-x64",
            "includePath": [
                "${workspaceFolder}/src",
                "/usr/local/include/**",
                "/usr/include/**"
            ],
            "defines": [],
            "cStandard": "c11",
            "cppStandard": "c++17",
            "forcedInclude": [
                "${workspaceFolder}/src/PCH.hpp"
            ]
        }
    ],
    "version": 4
}