C++ Visual Studio Code:如何配置 includePath 以获得更好的 IntelliSense 结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46258143/
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
Visual Studio Code: How to configure includePath for better IntelliSense results
提问by Niko P.
I am a complete beginner to using Visual Studio Code and I have no clue what I am doing.
我是使用 Visual Studio Code 的完全初学者,我不知道我在做什么。
I've searched around (maybe not enough), but I can't find just a simple explanation for someone like me on how to configure the c_cpp_properties.json
file that I am redirected to whenever I click on the yellow light bulb next to a line that is underlined with a green squiggle.
我已经四处搜索(可能还不够),但是我找不到像我这样的人的简单解释,说明如何配置c_cpp_properties.json
文件,每当我单击一行旁边的黄色灯泡时,我都会重定向到该文件带有绿色波浪线下划线。
I just want to know what to put in the .json
to make IntelliSense work properly.
我只想知道要放入什么.json
才能使 IntelliSense 正常工作。
回答by Oliver Whittlef
From C/C++ for Visual Studio Code (Preview):
来自C/C++ for Visual Studio Code(预览版):
Below you can see that the MinGW C++ include path has been added to browse.path for Windows:
下面您可以看到 MinGW C++ 包含路径已添加到 browse.path for Windows:
{
"name": "Win32",
"includePath": [
"${workspaceRoot}"
],
"defines": [
"_DEBUG",
"UNICODE"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"path": [
"${workspaceRoot}",
"C:\MinGW\lib\gcc\mingw32\6.3.0\include\c++"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
回答by Sushant Chaudhary
From the official documentation of the C/C++ extension:
来自 C/C++ 扩展的官方文档:
Configuring includePath for better IntelliSense results
配置 includePath 以获得更好的 IntelliSense 结果
If you're seeing the following message when opening a folder in Visual Studio Code, it means the C++ IntelliSense engine needs additional information about the paths in which your include files are located.
如果在 Visual Studio Code 中打开文件夹时看到以下消息,则意味着 C++ IntelliSense 引擎需要有关包含文件所在路径的其他信息。
Where are the include paths defined?
包含路径在哪里定义?
The include paths are defined in the "includePath"
setting in a file called c_cpp_properties.jsonlocated in the .vscode directory in the opened folder.
包含路径在位于打开文件夹的 .vscode 目录中的"includePath"
名为c_cpp_properties.json的文件中的设置中定义。
You can create or open this file by either using the "C/Cpp: Edit Configurations"
command in the command palette or by selecting "Edit "includePath" setting"
in the light bulb menu (see the screenshot below). The quickest way to locate a light bulb is to scroll to the top of the source file and click on any green squiggle that shows up under a #include statement.
您可以使用"C/Cpp: Edit Configurations"
命令面板中的命令或"Edit "includePath" setting"
在灯泡菜单中选择来创建或打开此文件(请参见下面的屏幕截图)。找到灯泡的最快方法是滚动到源文件的顶部,然后单击 #include 语句下显示的任何绿色波浪线。
When a folder is opened, the extension attempts to locate your system headers based on your operating system, but it does not know about any other libraries that your project depends on. You can hover over the green squiggles or open the Problems window to understand which headers the IntelliSense engine is unable to open - sometimes it's the dependent headers that can't be located.
打开文件夹时,扩展程序会尝试根据您的操作系统定位您的系统标头,但它不知道您的项目所依赖的任何其他库。您可以将鼠标悬停在绿色波浪线上或打开“问题”窗口以了解 IntelliSense 引擎无法打开哪些标头 - 有时是无法定位的相关标头。
How can I specify the include paths?
如何指定包含路径?
You can specify the remaining paths using one of the techniques described below.
您可以使用下述技术之一指定剩余路径。
Use compile_commands.json file to supply includePaths and defines information
The extension can get the information for
"includePath"
and"defines"
from a compile_commands.json file, which can be auto-generated by many build systems such as CMake and Ninja. Look for the section where your current configuration is defined (by default there's one configuration per operating system, such as "Win32 or "Mac"), and set the"compileCommands"
property in c_cpp_properties.jsonto the full path to your compile_commands.json file and the extension will use that instead of the"includes"
and"defines"
properties for IntelliSense.Use the light bulb suggestions to auto-resolve includePath
The first thing to try is to leverage the light bulb path suggestions to auto-resolve the include paths. When you open a folder, the extension will recursivelysearch for potential include paths that match the header files your code is using based on the paths set by the
"browse.path"
setting in c_cpp_properties.json. Click on the green squiggles under #include statements and you'll see a light bulb offering suggestions of paths that will allow IntelliSense to resolve the included file.If you don't see path suggestions in the light bulb, try adding the root folder where the headers are likely located in to the
"browse.path"
setting in c_cpp_properties.json. This allows the extension to recursivelysearch in these folders and offer more suggestions in the light bulb as the search process goes on.Manually add include paths
If none of the above fully resolves the paths, you could manually specify the paths to the headers that your project depends on in the c_cpp_properties.jsonfile. Look for the section where your current configuration is defined (by default there's one configuration per OS, such as "Win32 or "Mac"), and add your paths in the
"includePath"
setting and defines in the"defines"
setting. For example, the following screenshot shows a snippet of the file specifying path for the Mac configuration.Also, for MinGW, as the documentation of the extension explainsyou may ask gcc/g++to list its own include files:
gcc -v -E -x c++ nul
使用 compile_commands.json 文件提供 includePaths 和定义信息
该扩展可以得到的信息
"includePath"
,并"defines"
从compile_commands.json文件,它可以自动生成许多构建系统,如CMake和忍者。查找定义当前配置的部分(默认情况下,每个操作系统有一个配置,例如“Win32 或”Mac”),并将c_cpp_properties.json 中的"compileCommands"
属性设置为compile_commands.json 文件的完整路径和扩展将使用它而不是IntelliSense的和属性。"includes"
"defines"
使用灯泡建议自动解析 includePath
首先要尝试的是利用灯泡路径建议来自动解析包含路径。当您打开一个文件夹时,扩展程序将根据c_cpp_properties.json 中的设置设置的路径递归搜索与您的代码正在使用的头文件匹配的潜在包含路径。单击#include 语句下的绿色波浪线,您将看到一个灯泡,提供允许 IntelliSense 解析包含文件的路径建议。
"browse.path"
如果您在灯泡中没有看到路径建议,请尝试将标头可能位于的根文件夹添加到c_cpp_properties.json的
"browse.path"
设置中。这允许扩展程序在这些文件夹中递归搜索,并随着搜索过程的进行在灯泡中提供更多建议。手动添加包含路径
如果以上都不能完全解析路径,您可以在c_cpp_properties.json文件中手动指定项目所依赖的头文件的路径。查找定义您当前配置的部分(默认情况下每个操作系统有一个配置,例如“Win32 或”Mac”),并在
"includePath"
设置中添加您的路径并在设置中定义"defines"
。例如,以下屏幕截图显示了指定 Mac 配置路径的文件片段。此外,对于 MinGW,正如扩展的文档所解释的那样,您可以要求gcc/g++列出它自己的包含文件:
gcc -v -E -x c++ nul
Verify the include paths are correctly resolved
验证包含路径是否正确解析
There are two ways to verify that the include paths are correctly resolved:
有两种方法可以验证包含路径是否正确解析:
- The green squiggles in the source file are no longer showing
- Error messages are cleared in the Problems window
- 源文件中的绿色波浪线不再显示
- 在问题窗口中清除错误消息
This indicates that the IntelliSense engine has got the include paths resolved so you can start enjoying the full IntelliSense for your C or C++ code for the current translation unit. Note that you may still see errors on other files if they belong to a different translation unit that requires additional include paths to be configured.
这表明 IntelliSense 引擎已解析包含路径,因此您可以开始享受当前翻译单元的 C 或 C++ 代码的完整 IntelliSense。请注意,如果其他文件属于需要配置其他包含路径的不同翻译单元,您可能仍会在其他文件上看到错误。