VSCode c ++ task.json包含路径和库

Die*_*zzi 5 c++ g++ build visual-studio-code

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

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

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

链接

  1. 我是否通过指定构建工具的args中的所有库和包含目录来正确设置VSCode?还是应该以其他方式进行?
  2. 有人可以用不同的词来解释includePath和browser之间的区别吗?解释链接对我来说并不完全清楚

这是我的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
}
Run Code Online (Sandbox Code Playgroud)

和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"
}
Run Code Online (Sandbox Code Playgroud)

这是一个简单的问题,但是我是VSCode的新手(对不起)。

Sco*_*eak 18

1. 我是否正确设置了 VSCode?

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

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

作为一个非常常见的例子,你可以使用GNU Maketasks.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
Run Code Online (Sandbox Code Playgroud)

这告诉make如何建立test-sdlmain2.cpp,即通过运行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"
}
Run Code Online (Sandbox Code Playgroud)

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

2. 有人能解释一下... includePath 和浏览吗?

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

要确保您使用的是 Intellisense 而不是 Tag Parser,请进入 File ? 喜好 ?设置?C/C++ ? “C_Cpp:Intelli Sense 引擎”并确保它是“默认”而不是“标签解析器”。请注意,此设置存储在settings.json而不是 中c_cpp_properties.json


小智 9

我也尝试使用库,至少现在这有效(我在 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
}
Run Code Online (Sandbox Code Playgroud)

在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
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)


小智 2

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

我最终在我的tasks.json中这样做了

 "tasks": [
    {
        "type": "shell",
        "label": "g++ build active file",
        "command": "${workspaceFolder}/buildMysorcery.sh",
        "options": {
            "cwd": "/usr/bin"
        }
    },
Run Code Online (Sandbox Code Playgroud)

(似乎您无法转义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
Run Code Online (Sandbox Code Playgroud)

将fullpathtodir替换为您的路径。