C++ Visual Studio 代码包含路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37522462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:42:04  来源:igfitidea点击:

Visual Studio Code includePath

c++cvisual-studio-code

提问by stdio.h

I'm trying to build C/C++ in Visual Studio Code. I installed C/C++ and all the relevant extensions.

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

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

But there's a green line under #include <stdio.h>saying "Add include path to settings". When I click it, it moves over to "c_cpp_properties.json".

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

How and where can I add include paths in the configurations below?

我如何以及在哪里可以在下面的配置中添加包含路径?

"configurations": [
    {
        "name": "Mac",
        "includePath": ["/usr/include"]
    }
]

采纳答案by Niall

How and where can I add include paths in the configurations below?

我如何以及在哪里可以在下面的配置中添加包含路径?

The list is a string array, hence adding an include path would look something like;

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

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

Source; cpptools blog 31 March 2016.

来源; cpptools 博客 2016 年 3 月 31 日

The linked source has a gif showing the format for the Win32 configuration, but the same applies to the others.

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

The above sample includes the SDK (OSX 10.11) path if Xcode is installed.

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

NoteI find it can take a while to update once the include path has been changed.

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

The cpptools extension can be found here.

可以在此处找到cpptools 扩展。

Further documentation (from Microsoft) on the C++ language support in VSCode can be found here.

可以在此处找到有关VSCode 中 C++ 语言支持的更多文档(来自 Microsoft)。



During 2018, the C++ extension added another option to the configuration compilerPathof the c_cpp_properties.jsonfile;

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

compilerPath(optional) The absolute path to the compiler you use to build your project. The extension will query the compiler to determine the system include paths and default defines to use for IntelliSense.

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

If used, the includePathwould not be needed since the IntelliSense will use the compiler to figure out the system include paths.

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



For the sake of preservation (from the discussion), the following are basic snippets for the contents of the tasks.json file to compile and execute either a C++ file, or a C file. They allow for spaces in the file name (requires escaping the additional quotes in the json using \"). The shell is used as the runner, thus allowing the compilation (clang...) and the execution (&& ./a.out) of the program. It also assumes that the tasks.json "lives" in the local workspace (under the directory .vscode). Further task.json details, such as supported variables etc. can be found here.

为了保存(来自讨论),以下是 tasks.json 文件内容的基本片段,用于编译和执行 C++ 文件或 C 文件。它们允许文件名中包含空格(需要使用 转义 json 中的附加引号\")。壳使用作为流道,从而允许汇编(clang...)和执行(&& ./a.out该程序的)。它还假设 tasks.json “存在于”本地工作区中(在目录 .vscode 下)。可以在此处找到更多 task.json 详细信息,例如支持的变量等。

For C++;

对于 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"]
}

For C;

对于 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... 
}

回答by Vitox

For everybody that falls off google, in here, this is the fix for VSCode 1.40 (2019):

对于从谷歌上掉下来的每个人,这里是 VSCode 1.40 (2019) 的修复程序:

Open the global settings.json: File > Preferences > Settings

打开全局 settings.json:文件 > 首选项 > 设置

Open the global settings.json: File >Preferences >Settings

打开全局 settings.json:文件 >首选项 >设置

Then select the tab 'User', open the section 'Extensions', click on 'C/C++'. Then scroll the right panel till you find a 'Edit in settings.json' button.

然后选择“用户”选项卡,打开“扩展”部分,单击“C/C++”。然后滚动右侧面板,直到找到“在 settings.json 中编辑”按钮。

Then select the tab 'User', open the section 'Extensions', click on 'C/C++'. Then scroll the right panel till you find a 'Edit in settings.json' button.

然后选择“用户”选项卡,打开“扩展”部分,单击“C/C++”。 然后滚动右侧面板,直到找到“在 settings.json 中编辑”按钮。

Last, you add the "C_Cpp.default.includePath" section. The code provided there is from my own system (Windows 7). You can use it as a base for your own libraries paths. (Remember to change the YOUR USERNAMEto your correct system (my case windows) username)
(edit info: There is a problem with the recursion of my approach. VSCode doesn't like multiple definitions for the same thing. I solved it with "C_Cpp.intelliSenseEngine": "Tag Parser")

最后,添加“C_Cpp.default.includePath”部分。那里提供的代码来自我自己的系统(Windows 7)。您可以将其用作您自己的库路径的基础。(记住将 更改YOUR USERNAME为您正确的系统(我的案例窗口)用户名)
(编辑信息:我的方法的递归存在问题。VSCode 不喜欢同一事物的多个定义。我用 解决了它"C_Cpp.intelliSenseEngine": "Tag Parser"

Last, you add the "C_Cpp.default.includePath" section. The code provided there is from my own system (Windows 7). You can use it as a base for your own libraries paths. (Remember to change the YOUR USERNAME to your correct system (my case windows) username)

最后,添加“C_Cpp.default.includePath”部分。 那里提供的代码来自我自己的系统(Windows 7)。 您可以将其用作您自己的库路径的基础。 (记住将您的用户名更改为您正确的系统(我的案例窗口)用户名)

the code before line 7, on the settings.json has nothing to do with arduino or includePath. You may not copy that...

settings.json 上第 7 行之前的代码与 arduino 或 includePath 无关。你不能复制那个...

JSON section to add to settings.json:

要添加到 settings.json 的 JSON 部分:

"C_Cpp.default.includePath": [
        "C:/Program Files (x86)/Arduino/libraries/**",
        "C:/Program Files (x86)/Arduino/hardware/arduino/avr/cores/arduino/**",
        "C:/Program Files (x86)/Arduino/hardware/tools/avr/avr/include/**",
        "C:/Program Files (x86)/Arduino/hardware/tools/avr/lib/gcc/avr/5.4.0/include/**",
        "C:/Program Files (x86)/Arduino/hardware/arduino/avr/variants/standard/**",
        "C:/Users/<YOUR USERNAME>/.platformio/packages/framework-arduinoavr/**",
        "C:/Users/<YOUR USERNAME>/Documents/Arduino/libraries/**",
        "{$workspaceFolder}/libraries/**",
        "{$workspaceFolder}/**"
    ],
"C_Cpp.intelliSenseEngine": "Tag Parser"

回答by ryenus

For Mac users only installed Command Line Tools instead of Xcode, check the /Library/Developer/CommandLineTools/directory, for example::

对于 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"
    ]
}]

You probably need to adjust the path if you have different version of Command Line Tools installed.

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

Note: You can also open/generate the c_cpp_properties.jsonfile via the C/Cpp: Edit Configurationscommand from the Command Palette (??P).

注意:您还可以c_cpp_properties.json通过C/Cpp: Edit Configurations命令面板 (??P) 中的命令打开/生成文件。

回答by regomodo

In your user settings add:

在您的用户设置中添加:

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

回答by Bob Brown

The best way to configure the standard headers for your project is by setting the compilerPathproperty to the configurations in your c_cpp_properties.jsonfile. It is not recommended to add system include paths to the includePathproperty.

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

Another option if you prefer not to use c_cpp_properties.jsonis to set the C_Cpp.default.compilerPathsetting.

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

回答by Avi Kumar Singh

I solved this issue by uninstalling Visual Studio that I had installed recently.

我通过卸载最近安装的 Visual Studio 解决了这个问题。

回答by 100rabh

This answer maybe late but I just happened to fix the issue. Here is my c_cpp_properties.json file:

这个答案可能晚了,但我碰巧解决了这个问题。这是我的 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

}

}