【问题标题】:Visual studio code SFML no such file or directoryVisual Studio 代码 SFML 没有这样的文件或目录
【发布时间】:2020-04-07 04:02:39
【问题描述】:

您好,我一直在尝试通过使用 Visual studo 代码来学习 c++ SFML。 在观看了如何在 C++ 中安装 sfml 的教程后,我已经完成了所有设置。 但是,问题是当我尝试编译时它给了我这个错误: “main.cpp:2:10:致命错误:SFML/Graphics.hpp:没有这样的文件或目录”。 我已经阅读了许多指南,但似乎都没有工作。

这是我的代码:

#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;
}

希望有人能帮我解决这个问题。

tasks.json:

{
    "tasks": [
        {
            "type": "shell",
            "label": "g++.exe build active file",
            "command": "C:/mingw32/bin/g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "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"
            }
        }
    ],
    "version": "2.0.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
}

【问题讨论】:

  • 您使用什么构建系统以及您的编译器标志是什么?
  • 我应该发送什么 .json 文件。
  • 您需要为 SFML 添加包含目录。如何做到这一点取决于你如何编译你的项目。
  • 添加了 json 文件
  • @super 我应该添加哪些目录?

标签: c++ visual-studio-code sfml


【解决方案1】:

您还需要将包含目录添加到构建说明中。

"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 还为这些构建系统提供了一个不错的插件。

【讨论】:

  • 我需要创建一个新的 json 文件还是打开一个现有的文件?
  • @BartekDusza 我不确定你的意思。
  • 嗯,我很新,我在问我应该把 .json 代码放在哪个文件中
  • @BartekDusza 这将在tasks.json 的编译器参数中。但是 VS 代码不是构建系统。您应该为自己配备适当的工具。
  • 非常感谢
【解决方案2】:

我添加了 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"
            }
        }
    ]
}

它仍然无法正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2014-02-25
    相关资源
    最近更新 更多