# include 在 vscode 中检测到的错误

我在我的 C + + 项目中使用 Visual Studio 代码。我安装了 Microsoft C/C + + VS 代码扩展。我得到了以下错误:

#include errors detected. Please update your includePath. IntelliSense features for this translation unit (/path/to/project/file.cpp) will be provided by the Tag Parser.

499827 次浏览

答案就在这里: 如何使用 C/Cpp 扩展并向配置添加 include 路径

单击灯泡,然后编辑打开的 JSON 文件。选择与您的平台对应的块(有 MacLinuxWin32-ms-vscode)。Cpptools 版本: 3)。更新 includePath中的路径(使用 VS 代码编译时的问题)或 browse.paths(使用 VS 代码导航时的问题)或两者兼而有之。

感谢@Francesco Borzì,我将在这里附上他的回答:

你必须 Left 🖰点击旁边的弯弯曲曲的代码行灯泡。

如果找不到 #include文件或其依赖项之一,还可以单击 include 语句下的红色波浪线,查看如何更新配置的建议。

enter image description here

我正在尝试一个 hello world 程序,这句话:

#include <stdio.h>

下划线是绿色的,我试过了:

  1. 删除行
  2. 重写台词
  3. 单击黄色灯泡并选择更新

修正了错误警告。我不知道这能不能解决问题。然后我通过 Windows 10上的 linux 虚拟机进行编译

关闭并重新打开 VisualStudio 代码。

I ended up here after struggling for a while, but actually what I was missing was just:

如果找不到 # include 文件或其依赖项之一,还可以单击 include 语句下的红色波浪线,查看如何更新配置的建议。

enter image description here

来源: https://code.visualstudio.com/docs/languages/cpp#_intellisense

在关闭和重新打开 VS 之后,应该解决这个问题。

错误消息“ Please update your include-Path”并不一定意味着 includePath实际上存在问题。问题可能是 VSCode 使用了错误的编译器或错误的 IntelliSense 模式。我已经在 this answer中写了关于如何排除故障并将您的 VSCodeC + + 配置与您的编译器和项目对齐的说明。

  • 对于 一个 href = “ https://code.visalstudio.com/docs/cpp/config-mingw”rel = “ nofollow norefrer”> Windows :

1. 安装 Mingw-w64

2. 然后为您的帐户编辑 环境变量“ C: mingw-w64 x86 _ 64-8.1.0-win32-seh-rt _ v6-rev0 mingw64 bin”

3. 重新装弹

  • 对于 MAC

    1. 打开搜索,命令 + shift + P,运行代码“ c/c + + edit configuration (ui)”

    2. 打开文件 c _ cpp _ properties. json 并将 include-Path 从“ ${ workspaceFolder }/* *”更新为“ ${ workspaceFolder }/inc”

视窗:

  1. 请将此目录添加到您的环境变量(路径) :

C: mingw-w64 x86 _ 64-8.1.0-win32-seh-rt _ v6-rev0 mingw64 bin

  1. 对于检测到的“包含”错误,请指出“包含”文件夹的路径

“ include Path”: [ “ C:/mingw-w64/x86 _ 64-8.1.0-win32-seh-rt _ v6-rev0/mingw64/include/”]

, as this is the path from where the compiler fetches the library to be included in your program.

如果有人有这个问题,也许你只需要安装构建-必不可少的。

apt install build-essential
  • 鼠标左键点击错误灯泡行
  • Edit Include path
  • 然后这个窗口弹出

enter image description here

  • 设置 Compiler path

在我的例子中,我不需要关闭整个 VS-Code,关闭打开的文件(有时甚至保存它)就解决了这个问题。

从设置中搜索到 c_cpp_properties.json文件

{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "c++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}

如下所示更改编译器路径

"compilerPath": "/usr/bin/g++",

对我来说,使用 Ubuntu,我只需要安装 gcc 来解决这个问题。

sudo apt install gcc

然后,将编译器路径设置为 gcc。转到 c _ cpp _ properties. json 文件,设置:

"compilerPath": "/usr/bin/gcc"

如果你正在使用 cmake-tools,错误信息显示 configurationProvider出了问题,那么你有两个选择:

  1. 使用 ms-vscode.cpptools代替 ms-vscode.cmake-tools
  2. CMakeLists.txt中定义标头

选择1: 使用 ms-vscode.cpptools而不是 ms-vscode.cmake-tools

  1. 打开 < strong > c _ cpp _ properties. json 。(windows 上的 windows 键或者 mac + shift + p 上的 cmd 键,输入“ c/c + + edit configuration”并选择“ json”。
  2. 输入 < strong > ms-vscode. cpptools 作为 < strong > configuration ationProvider 的值,而不是 Ms-vscode. cmake-tools或其他值。

更换 < strong > configuration ationProvider 后的情况:

enter image description here

另一个重要的配置是 include 路径。假设您的配置是正确的。也许就像跟随

enter image description here

备选案文2: 在 CMakeLists.txt中定义标头

configurationProviderc_cpp_properties.json中设置为 ms-vscode.cmake-tools时,VS 代码使用 CMakeLists.txt中定义的 include 信息,而不是读取 VS 代码中的 includePath配置。

So you need to setup the include path correctly:

  1. 使用 Include _ directory 包括 _ 目录命令(而不是 target _ include _ directory 命令)定义标头
  2. 配置项目以反映在前一步中发生的更改。

另一种解决方案是在远程 WSL 中打开 VS Code,如果您打算用 g + + 编译文件的话。只需关闭 VS Code 并打开 WSL,然后键入 code .。文件资源管理器显示 VS Code 现在正在 WSL 的上下文中运行,标题栏为 [ WSL: Ubuntu ]。但是要确保在 WSL 上安装了 GNU compiler tools and the GDB debugger

source: https://code.visualstudio.com/docs/cpp/config-wsl

以防您在 VS 代码中将代码复制并粘贴到新文件中

请删除 #include <iostream>并尝试再次构建。

我很幸运地从 c _ cpp _ properties 中删除了注释。Json 在。Vscode 文件夹。默认情况下,json 文件中不允许使用注释,而且不能简单地重命名它。Jsonc.参考 在 VS 代码中,禁用“在 JSON 中不允许注释”错误

My header file was in include/head.h, code in src/code.cpp. I wrote

#include "head.h"

得到了这个错误。改为

#include "../include/head.h"

fixed it.

我解决了这个问题,一旦我删除 configurationProvider节点和设置 cStandardcppStandard设置为默认值:

{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}",
"${workspaceFolder}/**",
"${workspaceFolder}/test",
"/opt/qt5/include/QtCore",
"${workspaceFolder}/test"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "linux-clang-x64"
}
],
"version": 4
}

我用的是 Macbook M1 Pro,我的 C + + 文件上到处都是红色的弯弯曲曲的错误行。为了解决这个问题,我这样做了:

  1. 打开首选项-> 设置
  2. 搜索 intelliSenseEngine
  3. 将“ C _ Cpp: Intelligi Sense Engine”从 Default更改为 Tag Parser

或者,您可以在项目根目录中创建一个 .vscode文件夹,然后创建一个 settings.json,其内容为

{
"C_Cpp.intelliSenseEngine" : "Tag Parser"
}

理想情况下,您应该在同一个文件夹中有一个具有正确设置的 c_cpp_properties.json文件。

注意: 我是从 Github 给你得到这个想法的。

安装 c/c + + 扩展后,将在. vscode 文件夹中创建两个文件。

打开 c _ cpp _ properties. json 文件并将这个键-值对粘贴到配置对象中(如果它不存在的话)

"configurationProvider": "ms-vscode-cpptools"

如果它已经存在于对象中,则查看值部分是否为 ms-vscode-cmaketools。如果是这样,用上面的行替换现有的行。

这将允许您执行 cpp 文件和 c 文件。

I solved the error on my Mac by just clicking on the Edit "include path settings" and changing the compiler path to /usr/bin/clang.