VSCode c task.json 包含路径和库

新手上路,请多包涵

IntelliSense 使用 c_cpp_properties.json >> includePath 来查找自动完成的标头,但我注意到我仍然需要在 task.json >> tasks >> args 中指定包含路径来构建。

我在文档中发现 includePath 与我在“-I”中指定的路径几乎相同:

您为此设置指定的路径与您通过 -I 开关发送到编译器的路径相同。当您的源文件被解析时,IntelliSense 引擎将在尝试解析它们时将这些路径添加到您的#include 指令指定的文件中。这些路径不会被递归搜索。*

关联

  1. 我是否通过在构建任务的 args 中指定所有库和包含目录来正确设置 VSCode?还是应该以不同的方式完成?
  2. 有人可以用不同的词解释 includePath 和 browse 有什么区别吗?解释链接对我来说并不完全清楚

这是我的 c_cpp_properties.json 的示例:

 {
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/github/dependencies/SDL2-2.0.8/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64",
            "browse": {
                "path": [
                    "${workspaceFolder}/**"
                ]
            }
        }
    ],
    "version": 4
}

和task.json:

 {
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "main2.cpp",
                "-ID:\\github\\dependencies\\SDL2-2.0.8\\include",
                "-LD:\\github\\dependencies\\SDL2-2.0.8\\lib\\x64",
                "-lSDL2main","-lSDL2", "-lopengl32",
                "-o",
                "test-sdl"
            ]
        }
    ],
    "group": {
        "kind": "build",
        "isDefault": true
    },
    "problemMatcher":"$gcc"
}

是一个简单的问题,但我是 VSCode 的新手(抱歉)。

原文由 Diego Trazzi 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.7k
1 个回答

这就是我在 Mac(MacBook Pro 2015,macOS Catalina)上的 VS Code 中使用 clang 包含 OpenGL“GLWF”和“glad”库的方式。而且我有智能感知,可以构建和调试。

include/glad/glad.h - 要包含的库文件

src/helloworld.cpp - 主文件

/* Ask for an OpenGL Core Context */
#define GLFW_INCLUDE_GLCOREARB
#include <GLFW/glfw3.h>
#include <glad/glad.h>

#define BUFFER_OFFSET(i) ((char *)NULL + (i))

int main(int argc, char **argv)
{
    GLFWwindow *window;

    /* Initialize the library */
    if (!glfwInit())
    {
        return -1;
    }

#ifdef __APPLE__
    /* We need to explicitly ask for a 3.2 context on OS X */
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(1280, 720, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the buffers

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

.vscode/c_cpp_properties.json

 {
  "configurations": [
    {
      "name": "Mac",
      "includePath": ["${workspaceFolder}/src/", "${workspaceFolder}/include/"],
      "defines": [],
      "macFrameworkPath": [
        "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
      ],
      "compilerPath": "/usr/bin/clang",
      "cStandard": "c11",
      "cppStandard": "c++17",
      "intelliSenseMode": "${default}",
      "browse": {
        "limitSymbolsToIncludedHeaders": true,
        "databaseFilename": ""
      }
    }
  ],
  "version": 4
}

.vscode/launch.json

 {
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) Launch",
            "type": "lldb",
            "request": "launch",
            "program": "${workspaceFolder}/build/helloworld.out",
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

.vscode/tasks.json (您需要包含实现文件 include/glad.c ,不仅是标题)

 {
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build with Clang",
            "type": "shell",
            "command": "clang++",
            "args": [
                "-std=c++17",
                "-stdlib=libc++",
                "-lglfw3",
                "--include-directory=include/",
                "--include=include/glad.c",
                "-framework",
                "OpenGL",
                "-framework",
                "IOKit",
                "-framework",
                "Cocoa",
                "src/helloworld.cpp",
                "-o",
                "build/helloworld.out",
                "--debug"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

原文由 Michael Klishevich 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题