【问题标题】:VSCode c++ task.json include path and librariesVSCode c++ task.json 包含路径和库
【发布时间】:2022-01-15 03:20:43
【问题描述】:

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

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

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

link

  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 的新手(抱歉)。

【问题讨论】:

标签: c++ build visual-studio-code g++


【解决方案1】:

1。我是否正确设置了 VSCode?​​h3>

主要是。您必须两次指定包含路径(一次在 c_cpp_properties.json 中,再次在描述您的构建的文件中)这一事实是不可避免的。在 VSCode 中,构建系统和编辑器互不理解,都需要这些信息。相比之下,使用 Visual Studio(没有“代码”),只需指定一次路径;这是使用“真正的”集成开发环境的好处之一。 (但也有缺点;我并不是要阻止您使用 VSCode。)

但是,我不建议将包含路径直接放入tasks.json。相反,通常有一个单独的构建系统,可以从命令行调用,然后tasks.json 也调用该命令。

作为一个非常常见的示例,您可以使用 GNU Make 并将当前的 tasks.json 替换为这个(未经测试!)Makefile:

test-sdl: main2.cpp
    g++ -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

这告诉make 如何从main2.cpp 构建test-sdl,即通过运行所示的g++ 命令。 (我故意让这个 Makefile 保持非常简单,因为问题与 Makefile 无关;请注意,真正的 Makefile 会为了更好的组织而分解,并且反斜杠可能需要调整。)

无论如何,您的tasks.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": "make",   // <-- changed
            "args": []           // <-- changed
        }
    ],
    "group": {
        "kind": "build",
        "isDefault": true
    },
    "problemMatcher":"$gcc"
}

这更好,因为您没有将关键的构建信息锁定在只有 VSCode 才能理解的文件中。

2。有人可以解释一下... includePath 和浏览吗?

VSCode 有两种不同的系统来理解 C++ 代码。有较旧的“标签解析器”,它使用browse.path,以及较新的“智能感知”,它使用includePath。在这一点上(2019-08-30,VSCode 1.37.1),我的理解基本上每个人都应该使用更新的 Intellisense 系统,因为它提供了更准确的信息,并且至少应该是成熟的。因此,您应该能够简单地忽略 browse.path

要确保您使用的是 Intellisense 而不是 Tag Parser,请进入 File → Preferences → Settings → C/C++ → “C_Cpp: Intelli Sense Engine”并确保它是“Default”而不是“Tag Parser”。请注意,此设置存储在settings.json 而不是c_cpp_properties.json

【讨论】:

    【解决方案2】:

    我也尝试过使用库,至少现在可以使用(顺便说一句,我在 Windows 上): 在 c_cpp_properties.json 中,我有一个对包含目录的引用:

    {
        "configurations": [
            {
                "name": "Win32",
                "includePath": [
                    "C:\\ProgrammingLibraries\\SDL2-2.0.10\\include\\SDL2",
                    "${workspaceFolder}\\src\\include"
                ],
                "defines": [
                    "_DEBUG",
                    "UNICODE",
                    "_UNICODE"
                ],
                "compilerPath": "C:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
                "cStandard": "c11",
                "cppStandard": "c++17",
                "intelliSenseMode": "gcc-x64"
            }
        ],
        "version": 4
    }
    

    在 tasks.json 中,我有一个编译和一个链接器任务,以及一个同时运行的任务:

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            {
                "label": "Compiler",
                "type": "shell",
                "command": "g++",
                "args": [
                    "-c",
                    "${workspaceFolder}\\src\\main.cpp",
                    "-IC:\\ProgrammingLibraries\\SDL2-2.0.10\\include\\SDL2"
                ]
            },
            {
                "label": "Linker",
                "type": "shell",
                "command": "g++",
                "args": [
                    "${workspaceFolder}\\main.o",
                    "-o",
                    "${workspaceFolder}\\bin\\HelloSDL.exe",
                    "-LC:\\ProgrammingLibraries\\SDL2-2.0.10\\lib",
                    "-lmingw32",
                    "-lSDL2main",
                    "-lSDL2"
                ]
            },
            {
                "label": "Build HelloSDL",
                "dependsOn": [
                    "Compiler",
                    "Linker"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            }
        ]
    }
    

    【讨论】:

    • 我尝试了这种方法,它似乎有效。我遇到的唯一问题是,如果您在最后一个任务中声明所有依赖项,它们将并行执行,因此有时您最终会链接以前构建的 .o 文件。我建议只在最后一个任务中声明“链接器”依赖项,在“链接器”任务中声明“编译器”依赖项。这样,链接器就会等待编译器真正完成工作。
    【解决方案3】:

    我也是 VS Code 的新手。此外,我从未构建过更大的 C++ 项目。至少现在,我这样解决了这座建筑。 (见下文)

    我最终在我的 tasks.json

    中这样做了
     "tasks": [
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "${workspaceFolder}/buildMysorcery.sh",
            "options": {
                "cwd": "/usr/bin"
            }
        },
    

    (似乎您无法在 args 属性中转义空格,https://github.com/Microsoft/vscode/issues/36733),

    我删除了 args 属性并将命令属性设置为运行一个脚本(buildMysorcery.sh),它只是这样做

    #!/bin/bash
    
    g++ fullpathtodir/hello.cpp -Wall -g $(sdl2-config --cflags --libs) -o fullpathtodir/hello
    

    用你的路径替换 fullpathtodir

    【讨论】:

      【解决方案4】:

      这就是我在 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,不仅是headers)

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-19
        • 2019-04-21
        • 2022-08-20
        相关资源
        最近更新 更多