Visual Studio代码includePath

问题描述 投票:42回答:5

我正在尝试在Visual Studio Code中构建C / C ++。我安装了C / C ++和所有相关的扩展。

#include <stdio.h>
int main() {
    printf("Test C now\n");
    return 0;
}

但是在#include <stdio.h>下面有一条绿线说“添加包含设置路径”。当我单击它时,它会移动到“c_cpp_properties.json”。

我可以在下面的配置中添加包含路径的方式和位置?

"configurations": [
    {
        "name": "Mac",
        "includePath": ["/usr/include"]
    }
]
c++ c visual-studio-code
5个回答
40
投票

我可以在下面的配置中添加包含路径的方式和位置?

列表是一个字符串数组,因此添加一个包含路径看起来像;

"configurations": [
    {
        "name": "Mac",
        "includePath": ["/usr/local/include",
            "/path/to/additional/includes",
            "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include"
        ]
    }
]

资源; cpptools blog 31 March 2016

链接源有一个显示Win32配置格式的gif,但同样适用于其他配置。

如果安装了Xcode,上面的示例包含SDK(OSX 10.11)路径。

注意我发现更改包含路径后可能需要一段时间才能更新。

cpptools扩展可以是found here

有关VSCode中C ++语言支持的更多文档(来自Microsoft)可以是found here


在2018年期间,C ++扩展为compilerPath文件的配置c_cpp_properties.json添加了另一个选项;

compilerPath(可选)用于构建项目的编译器的绝对路径。扩展将查询编译器以确定系统包含路径和用于IntelliSense的默认定义。

如果使用,则不需要includePath,因为IntelliSense将使用编译器来确定系统包含路径。


为了保存(从讨论中),以下是tasks.json文件内容的基本片段,用于编译和执行C ++文件或C文件。它们允许文件名中的空格(需要使用\"转义json中的附加引号)。 shell使用as the runner,因此允许程序的编译(clang...)和执行(&& ./a.out)。它还假定tasks.json“生活”在本地工作空间中(在.vscode目录下)。进一步的task.json细节,如支持的变量等可以是found here

对于C ++;

{ 
    "version": "0.1.0", 
    "isShellCommand": true, 
    "taskName": "GenericBuild", 
    "showOutput": "always", 
    "command": "sh", 
    "suppressTaskName": false, 
    "args": ["-c", "clang++ -std=c++14 -Wall -Wextra -pedantic -pthread \"${file}\" && ./a.out"]
}

对于C;

{ 
    "version": "0.1.0", 
    "isShellCommand": true, 
    "taskName": "GenericBuild", 
    "showOutput": "always", 
    "command": "sh", 
    "suppressTaskName": false, 
    "args": ["-c", "clang -std=c11 -Wall -Wextra -pedantic -pthread \"${file}\" && ./a.out"] // command arguments... 
}

10
投票

对于Mac用户只安装了命令行工具而不是Xcode,请检查/Library/Developer/CommandLineTools/目录,例如::

"configurations": [{
    "name": "Mac",
    "includePath": [
        "/Library/Developer/CommandLineTools/usr/lib/clang/8.1.0/include/",
        "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1/tr1/",
        "/usr/include/c++/4.2.1",
        "/usr/local/include",
        "/usr/include"
    ]
}]

如果安装了不同版本的命令行工具,则可能需要调整路径。

Note:您还可以通过命令选项板(⇧⌘P)中的c_cpp_properties.json命令打开/生成C/Cpp: Edit Configurations文件。


1
投票

在您的用户设置中添加:

"C_Cpp.default.includePath":["path1","path2"]

1
投票

为项目配置标准头文件的最佳方法是将compilerPath属性设置为c_cpp_properties.json文件中的配置。建议不要将系统包含路径添加到includePath属性。

如果您不想使用c_cpp_properties.json,另一个选项是设置C_Cpp.default.compilerPath设置。


0
投票

这个答案可能很晚,但我碰巧解决了这个问题。这是我的c_cpp_properties.json文件:

{
"configurations": [
    {
        "name": "Linux",
        "includePath": [
            "${workspaceFolder}/**",                
            "/usr/include/c++/5.4.0/",
            "usr/local/include/",
            "usr/include/"
        ],
        "defines": [],
        "compilerPath": "/usr/bin/gcc",
        "cStandard": "c11",
        "cppStandard": "c++14",
        "intelliSenseMode": "clang-x64"
    }
],
"version": 4

}

© www.soinside.com 2019 - 2024. All rights reserved.