Search code examples
c++windowsvisual-studio-codeg++sfml

Unable to compile SFML C++ code with g++ from Visual Studio Code


I'm trying to compile some C++ code in Visual Studio Code but I want to use the SFML library and for some reason it just can't find my library. I am using the c/c++ extension which is configured like this

{
"configurations": [
    {
        "name": "Win32",
        "includePath": [
            "${workspaceFolder}/**", "C:\\lib\\SFML\\include\\**"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "compilerPath": "C:\\TDM-GCC-64\\bin\\g++.exe",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "clang-x64"
    }
],
"version": 4}

and I've configured a task following this other question which gives me something like this

{
"version": "2.0.0",
"tasks": 
[
    {
        "label": "Compilation",
        "type": "shell",
        "group": "build",
        "command": "g++",
        "args": 
        [
            "main.cpp",
            "-o",
            "GUImeOfLife.exe",
            "-IC:C:\\lib\\SFML\\include\\SFML",
            "-LC:C:\\lib\\SFML\\lib",
            "-lsfml-graphics",
            "-lsfml-window",
            "-lsfml-system"
        ],
        "problemMatcher": "$gcc"
    }
],
"presentation": {
    "echo": true,
    "reveal": "always",
    "focus": false,
    "panel": "shared",
    "showReuseMessage": true,
    "clear": true
}}

However, when I run the task on my code (simply the example code from the SFML library website), I get this message from the compiler:

Executing task: g++ main.cpp -o GUImeOfLife.exe -IC:C:\lib\SFML\include\SFML -LC:C:\lib\SFML\lib -lsfml-graphics -lsfml-window -lsfml-system
main.cpp:1:24: fatal error: Graphics.hpp: No such file or directory compilation terminated.
The terminal process terminated with exit code: 1

(The problematic line simply is #include <Graphics.hpp>)

What do I have to change to make it work?

Thanks


Solution

  • From your configuration file

    "-IC:C:\\lib\\SFML\\include\\SFML",
    

    The path you present have an extra and wrong C: added in front.

    The path should be

    "-IC:\\lib\\SFML\\include\\SFML",
    

    This typo is part of the -L option as well.