且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

vscode检测到"#include错误".请更新您的includePath

更新时间:2022-03-02 18:40:06

尽管问题提到了Arduino,但只要VSCode告诉您更新includePath",以下建议基本上都适用.

Although the question mentions Arduino, the following suggestions apply basically any time VSCode tells you to "update your includePath".

includePathc_cpp_settings.json中的属性,该属性位于您使用文件"→打开文件夹"在VSCode中打开的主文件夹的.vscode文件夹中.

The includePath is an attribute in c_cpp_settings.json, which is in the .vscode folder of the main folder you have opened in VSCode using File → Open Folder.

您可以直接编辑c_cpp_settings.json,但是使用"C/C ++配置GUI"通常更容易.为此,请打开命令面板(Ctrl + Shift + P)并运行"C/C ++:编辑配置(UI)".然后寻找包含路径"设置.

You can edit c_cpp_settings.json directly, but it is usually easier to use the "C/C++ Configurations GUI". To do that, open the Command Palette (Ctrl+Shift+P) and run "C/C++: Edit Configurations (UI)". Then look for the "Include path" setting.

includePath告诉VSCode(特别是 C/C ++扩展)解析#include "filename"指令时的位置.这样,VSCode可以查看那些文件中定义的符号的定义.

The includePath tells VSCode (specifically the IntelliSense component of the C/C++ extension) where to look when resolving #include "filename" directives. That allows VSCode to see definitions of symbols defined in those files.

一开始不是!在更改包含路径之前,如果尚未设置,请首先将编译器路径"设置为指向C/C ++编译器,然后将"IntelliSense模式"设置为与编译器尽可能匹配.

Not at first! Before changing the include path, if you haven't already, first set the "Compiler path" to point at your C/C++ compiler, and set "IntelliSense mode" to match the compiler as closely as possible.

您可能还需要调整Compiler参数,尤其是在编译器能够为多个目标生成代码的情况下,例如32位和64位代码. (如果您不知道这是什么意思,请先跳过.)

You may also need to adjust the Compiler arguments, particularly if the compiler is capable of generating code for multiple targets, for example, both 32-bit and 64-bit code. (If you don't know what that means, skip it at first.)

接下来,在命令面板中,运行"C/C ++:日志诊断".输出将显示您找到了哪个VSCode编译器,以及它在内置的include路径和预处理器定义中所检测到的内容.

Next, in Command Palette, run "C/C++: Log Diagnostics". The output will show you which compiler VSCode found and what it detected as its built-in include path and preprocessor defines.

然后,在shell中运行以下命令:

Then, run these commands in a shell:

  $ touch empty.c
  $ gcc -v -E -dD empty.c

在这里,我假设您正在使用gcc作为编译器.如果不是,请替换实际的编译器命令名称.如果您的编译器不是 GCC 的变体(例如,您使用的是Microsoft cl.exe编译器),您需要查看其文档或Google来查找打印预定义宏并包含路径的开关(例如,请参见

Here, I have assumed you are using gcc as your compiler. If not, substitute the actual compiler command name. If your compiler is not a variant of GCC (for example you are using the Microsoft cl.exe compiler), you'll need to look at its documentation or Google to find switches that print the predefined macros and include paths (e.g., see here for cl.exe).

将以上命令的输出与VSCode在其C/C ++诊断输出中显示的内容进行比较.希望它们非常相似.如果不是,请尝试调整编译器路径,IntelliSense模式或编译器参数.通过仅调整这三个设置使它们尽可能接近后,请继续下一步.

Compare the output of the above command to what VSCode shows in its C/C++ diagnostics output. Hopefully they are very similar. If not, try adjusting the Compiler path, IntelliSense mode, or Compiler arguments. Once you've gotten them as close as possible by adjusting just those three settings, go on to the next step.

如果编译器的内置配置与VSCode检测到的内容之间仍然存在重大差异,请通过(在C/C ++设置UI中)修改包含"路径,定义"和"C/C ++"标准字段来解决此问题.重新运行C/C ++日志诊断命令以查看效果.

If there are still significant differences between the compiler built-in configuration and what VSCode detects, fix that by (in the C/C++ settings UI) modifying the Include path, Defines, and C/C++ standard fields. Re-run the C/C++ Log Diagnostics command to see the effects.

可能不必添加所有预定义的预处理器符号.仅当存在依赖它们的 #ifdef 指令时,这才真正重要,并导致VSCode将错误的代码视为活动代码.我建议仅在浏览代码时看到VSCode错误的特定情况下才添加预定义符号.

It is probably not necessary to add all of the pre-defined preprocessor symbols. This really only matters if there are #ifdef directives that depend on them, and which are causing VSCode to see the wrong code as active. I suggest only adding predefined symbols if, while browsing your code, you see a specific case that VSCode gets wrong.

最后,如果您的项目的头文件位于编译器默认不搜索的位置,即通常必须传递 -D 参数也是如此,必须将其添加到定义"中.

Finally, if your project has header files in places that the compiler does not search by default, that is, you normally have to pass -I switches on the compiler command line, add those as well to the Include path. The same goes for any -D arguments, which must be added to the Defines.