Visual studio代码SFML没有此类文件或目录 您好,我一直在尝试使用Visual Studio代码学习C++ SFML。 在看教程后,如何在C++中安装SFML,我已经把所有的东西都设置好了。 但是,问题是当我试图编译时,它会给我以下错误: “main.cpp:2:10:致命错误:SFML/Graphics.hpp:没有这样的文件或目录”。 我看过很多指南,但似乎没有一本有效

Visual studio代码SFML没有此类文件或目录 您好,我一直在尝试使用Visual Studio代码学习C++ SFML。 在看教程后,如何在C++中安装SFML,我已经把所有的东西都设置好了。 但是,问题是当我试图编译时,它会给我以下错误: “main.cpp:2:10:致命错误:SFML/Graphics.hpp:没有这样的文件或目录”。 我看过很多指南,但似乎没有一本有效,c++,visual-studio-code,sfml,C++,Visual Studio Code,Sfml,这是我的密码: #include <SFML/Graphics.hpp> #include <iostream> int main() { sf::RenderWindow window(sf::VideoMode(1280,720),"Nareszcie"); while(window.isOpen()) { sf::Event event; while (window.pollEvent(event))

这是我的密码:

#include <SFML/Graphics.hpp>
#include <iostream>
int main() {
    sf::RenderWindow window(sf::VideoMode(1280,720),"Nareszcie");
    while(window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type==sf::Event::Closed)
            {
                window.close();

            }
            window.clear();
        }
    }
    return 0;
}
c_cpp_prperties.json:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:/SFML-2.5.1/include/**",
                "C:/SFML-2.5.1"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.18362.0",
            "compilerPath": "C:/mingw32/bin/g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "${default}"
        }
    ],
    "version": 4
}

您还需要将include目录添加到构建说明中

"args": [
    "-g",
    "${file}",
    "-o",
    "${fileDirname}\\${fileBasenameNoExtension}.exe",
    "-I",
    "C:/SFML-2.5.1/include/"
],
但是,您还需要将可执行文件链接到SFML库

"args": [
    "-g",
    "${file}",
    "C:/path/to/sfml/libsfml-graphic.a" // something like that
    "-o",
    "${fileDirname}\\${fileBasenameNoExtension}.exe",
    "-I",
    "C:/SFML-2.5.1/include/",
],
我的建议是使用一个合适的构建系统,比如CMake、meson或其他,它会自动完成这类事情。下面是一个CMake示例:

cmake_minimum_required(VERSION 3.14)
project(your-project CXX)

# creates your executable with two cpp file in it
add_executable(my-exe main.cpp otherfile.cpp)

# find sfml. Assume the command line argument -DCMAKE_PREFIX_PATH="C:/SFML-2.5.1"
find_package(SFML 2.5.1 COMPONENTS graphic REQUIRED)

# configure your project correctly. Take care of include directories and linking
target_link_libraries(my-exe PUBLIC sfml-graphic)

VSCode还为这些构建系统提供了一个不错的插件。

我添加了tasks.json文件的路径 下面是它的外观:

{
    "version": "2.0.0",
    "tasks": [

        {
            "type": "shell",
            "label": "g++.exe build active file",
            "command": "C:/mingw32/bin/g++.exe",
            "args": [
                "-g",
                "${file}",
                "C:/SFML-2.5.1/lib/libsfml-graphic.a",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-I",
                "C:/SFML-2.5.1/include"
            ],
            "options": {
                "cwd": "C:/mingw32/bin"
            }
        },
        {
            "type": "shell",
            "label": "cpp.exe build active file",
            "command": "C:\\mingw32\\bin\\cpp.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\mingw32\\bin"
            }
        }
    ]
}

它仍然不起作用。

您使用的是什么构建系统,您的编译器标志是什么?我应该发送什么.json文件。您需要为SFML添加include目录。如何做到这一点取决于编译项目的方式files@super我应该添加哪些目录?我需要创建一个新的json文件还是打开一个现有的json文件?@BartekDusza我不确定你的意思。我很新,我在问我应该将.json放在哪个文件中code@BartekDusza这将在编译器中
tasks.json中的参数。但是VS代码不是一个构建系统。你应该给自己配备合适的工具。非常感谢
{
    "version": "2.0.0",
    "tasks": [

        {
            "type": "shell",
            "label": "g++.exe build active file",
            "command": "C:/mingw32/bin/g++.exe",
            "args": [
                "-g",
                "${file}",
                "C:/SFML-2.5.1/lib/libsfml-graphic.a",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-I",
                "C:/SFML-2.5.1/include"
            ],
            "options": {
                "cwd": "C:/mingw32/bin"
            }
        },
        {
            "type": "shell",
            "label": "cpp.exe build active file",
            "command": "C:\\mingw32\\bin\\cpp.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\mingw32\\bin"
            }
        }
    ]
}