在vscode中包含sfml库“ sfml/graphics.hpp否此类文件或目录”海湾合作委员会

发布于 2025-02-06 09:31:41 字数 1772 浏览 1 评论 0原文

我已经看到了其他问题与此有关,而且我能找到的大多数问题也有一些关于使SFML进行编译和运行的方法,我希望有一种简单的方法可以简单地附加SFML/INCLADY DIRECTORY到编译器包括Intellisense代码完成的此功能,并且正确地知道SFML库的位置。 IntelliSense ScreenShot。((当文件保存时,您可以在屏幕上看到的额外错误消失了) 。 因此,最终,我想知道或有帮助的是让SFML库进行编译和运行,而无需Github的样板,而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
}

I've seen other question's relating to this and most if not all that I could find have some round about way of getting SFML to compile and run in VSCode I'm hoping that there is a way to simply append the SFML/include directory to the compilers includes this works with the intelliSense code completion and it correctly knows the location of the SFML Library. intelliSense screenshot.(The extra errors you can see in the screenshot disappear when the file is saved).
So ultimately what I would like to know or have help with is getting the SFML library to compile and run without the need for the boilerplate from GitHub that seems to be commonly used thank you.

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

This is my 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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿挽旧人 2025-02-13 09:31:42

在VS代码中,C_CPP_PROPERTIE.JSON指定编辑器本身使用的参数(例如编译器路径,C ++标准,Include Directories 用于IntelliSense用途;请参阅文档在这里),但没有指定构建任务 - 这就是工作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
            },
        }
    ]
}

在这里,我们创建了一个新的 task ,名为“编译SFML可执行文件”。此任务将main.cpp编译成main.exe,而给出g ++有关SFML的所有必要信息:include Directories(-i -i) flag),库目录(-l <​​/code>)和主SFML库本身用于链接器输入。具体来说,-l {library_name}链接lib {library_name} .a,在我们的情况下,是导入tubs的导入库,涉及动态库{library_name} -2.dll(此名称在导入库中编码),可以在a:/ssfml-2.5.1/include/bin/bin/文件夹中找到(假设您已安装了Mingw版本由于使用mingw g ++编译器,因此您应该拥有的SFML库。 {library_name}在这里提到sfml-windowsfml-systemsfml-graphics,通过这些>您的代码是您现在唯一需要的代码。请注意,在运行之前,必须将动态库复制到可执行文件夹(或其他系统动态加载程序可以找到它们的地方)。

您可以通过ctrl-shift-p-&gt;运行任务。任务:运行任务 - &GT;编译SFML可执行文件,然后在运行之后,在VS代码之外执行可执行文件。另外,您可以在VS代码中分别使用f5ctrl-f5分别运行可执行文件,如果有或不进行调试,该可执行文件将根据默认启动在运行之前构建可执行文件配置的prelaunchtask,默认情况下将其设置为我们的任务(因为“ isDefault”:true)。

另请注意,如果要进行调试构建/运行,则应链接到{library_name} -d,而不是{library_name}(并复制code> {library_name} -d-2.dll中的可执行文件夹,而不是运行)。使用适当的-l {library_name} -d flags创建单独的任务(例如“编译SFML可执行调试”)可能对此有用。然后,您可以在运行之前选择要执行的构建任务,通过删除“ ISDEFAULT”:TRUE从两种配置中以及使用ctrl-shift-p-&gt; C/C ++:{debug/run} c/c ++文件并从列表中选择适当的prelaunchtask的启动配置。

In VS Code, c_cpp_properties.json specifies parameters used by the editor itself (like compiler path, C++ standard, include directories for IntelliSense purposes; see documentation here), but doesn't specify build tasks - that is the job of tasks.json. In your .vscode folder, create tasks.json file like this:

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

Here, we created a new task named "Compile SFML executable". This task compiles main.cpp into main.exe, while giving g++ all the necessary information about SFML: include directories (-I flag), library directories (-L) and main SFML libraries themselves for linker input. Specifically, -l{library_name} links lib{library_name}.a, which is, in our case, import library of stubs referring to dynamic library {library_name}-2.dll (this name is coded inside import library), which can be found in A:/SFML-2.5.1/include/bin/ folder (assuming you installed MinGW version of SFML libraries, which you should have, since you use MinGW G++ compiler). {library_name} here refers to sfml-window, sfml-system and sfml-graphics, which, judging by your code, are the only ones you need right now. Note that dynamic libraries have to be copied to executable folder (or wherever else system dynamic loader will be able to find them) before running.

You can run the task by Ctrl-Shift-P -> Tasks: Run Task -> Compile SFML executable, and after that run resulting executable outside of VS Code. Alternatively, you can use F5 or Ctrl-F5 within VS Code to run executable with or without debugging respectively, where the executable will be built prior to run according to a default launch configuration's preLaunchTask, which by default is set to our task (because of "isDefault": true).

Also note that if you want to do debug builds/runs, you should link to {library_name}-d instead of {library_name} (and copy {library_name}-d-2.dll into executable folder to run) instead. Creating separate task (like "Compile SFML executable Debug") with appropriate -l{library_name}-d flags can be useful for this. You can then choose which build task to perform before run by removing "isDefault": true from both configurations and using Ctrl-Shift-P -> C/C++: {Debug / Run} C/C++ File and choosing launch configuration with appropriate preLaunchTask from the list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文