c++ - 在 VSCode 中包含 SFML 库 "SFML/Graphics.hpp no such file or directory"gcc

标签 c++ visual-studio-code gcc g++ sfml

我已经看到了与此相关的其他问题,而且我能找到的大多数(如果不是全部)都有一些让 SFML 在 VSCode 中编译和运行的方法,我希望有一种方法可以简单地附加 SFML/编译器的 include 目录包括这与 IntelliSense 代码完成一起工作,并且它正确地知道 SFML 库的位置。 intelliSense screenshot. (保存文件后,您在屏幕截图中看到的额外错误就会消失)。 因此,最终我想知道或获得帮助的是让 SFML 库编译和运行,而不需要来自 GitHub 的似乎常用的样板,谢谢。

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();
    }
}

这是我的 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
}

最佳答案

在 VS Code 中,c_cpp_properties.json 指定编辑器本身使用的参数(例如编译器路径、C++ 标准、用于 IntelliSense 目的的包含目录;请参阅文档 here ),但不指定构建任务 - 这是 tasks.json 的工作。在 .vscode 文件夹中,创建 tasks.json 文件,如下所示:

{
    "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
            },
        }
    ]
}

在这里,我们创建了一个名为“编译 SFML 可执行文件”的新任务。此任务将 main.cpp 编译为 main.exe,同时为 g++ 提供有关 SFML 的所有必要信息:包含目录 (-I 标志)、库目录 (-L) 和用于链接器输入的主 SFML 库本身。具体来说,-l{library_name}链接lib{library_name}.a,在我们的例子中,它是引用动态库{library_name}的 stub 导入库-2.dll(此名称在导入库中编码),可以在A:/SFML-2.5.1/include/bin/文件夹中找到(假设您安装了MinGW版本SFML 库,您应该拥有这些库,因为您使用 MinGW G++ 编译器)。这里的{library_name}指的是sfml-windowsfml-systemsfml-graphics,从您的代码是您现在唯一需要的。请注意,在运行之前,必须将动态库复制到可执行文件夹(或系统动态加载程序能够找到它们的其他位置)。

您可以通过Ctrl-Shift-P -> 任务:运行任务 -> 编译 SFML 可执行文件来运行任务,然后在 VS Code 之外运行生成的可执行文件。或者,您可以在 VS Code 中使用 F5Ctrl-F5 分别在带调试或不带调试的情况下运行可执行文件,其中可执行文件将在根据默认启动运行之前构建配置的 preLaunchTask,默认情况下设置为我们的任务(因为 "isDefault": true)。

另请注意,如果您想进行调试构建/运行,则应链接到 {library_name}-d 而不是 {library_name} (并复制 {library_name}-d-2.dll 进入可执行文件夹来运行)。使用适当的 -l{library_name}-d 标志创建单独的任务(例如“编译 SFML 可执行文件调试”)对此很有用。然后,您可以通过从两个配置中删除 "isDefault": true 并使用 Ctrl-Shift-P -> C/C++: {Debug/Run} C 来选择在运行之前执行哪个构建任务/C++ File 并从列表中选择具有适当 preLaunchTask 的启动配置。

关于c++ - 在 VSCode 中包含 SFML 库 "SFML/Graphics.hpp no such file or directory"gcc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72580240/

相关文章:

c++ - sql Column with multiple values(cpp文件中的查询实现)

ubuntu - 如何重置vscode的主题

reactjs - VS code 中是否有一个功能,可以打开父组件(又名传递 props 的组件)?

c - 功能和性能的返回值

java - 线程 "main"java.lang.UnsatisfiedLinkError 3 中的异常

在MinGW(使用MSYS)中为Windows 10编译GTK+-2.0程序

c++ - 运行 QT Creator 的项目 .exe 给出 0xc000007b 错误

android - java.lang.UnsatisfiedLinkError : dlopen failed: library "libopencv_java3.so" not found 错误

c++ - 在 STL 容器中存储 OpenCV Mat 对象时避免内存泄漏

VSCode 的 Python 自动导入扩展