Although the question mentions Arduino, the following suggestions apply basically any time VSCode tells you to "update your includePath".
What is includePath?
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. 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. 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.
So should I fiddle with includePath when VSCode tells me to?
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.
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.) 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. Then, run these commands in a shell:
$ touch empty.c
$ gcc -v -E -dD empty.c
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 ). 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.
Now adjust includePath if necessary
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. 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. 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.
"includePath": [
"<arduino ide installation folder>\\tools\\**",
"<arduino ide installation folder>\\hardware\\arduino\\avr\\**",
"<arduino ide installation folder>\\hardware\\tools\\**",
"<arduino ide installation folder>\\hardware\\arduino\\avr\\cores\\arduino"
]
7条答案
按热度按时间eagi6jfj1#
Although the question mentions Arduino, the following suggestions apply basically any time VSCode tells you to "update your includePath".
What is includePath?
The
includePath
is an attribute inc_cpp_settings.json
, which is in the.vscode
folder of the main folder you have opened in VSCode using File → Open Folder.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.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.So should I fiddle with includePath when VSCode tells me to?
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.
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.)
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.
Then, run these commands in a shell:
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 Microsoftcl.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 forcl.exe
).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.
Now adjust includePath if necessary
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.
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.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.hkmswyz62#
这是由于扩展在初始化时缺少一些includepath
将缺少的行添加到c_cpp_properties.json中
同时在“配置”下添加
"defines": [ "USBCON" ]
,以使串行类与intellisense一起工作bhmjp9jg3#
尝试使用platformIO扩展,它会让你的生活变得更容易。就我个人而言,我在Arduino和ESP32项目中使用带有platformIO的VScode。
enyaitl34#
我刚刚成功地浪费了一个小时找到解决这个问题的堆栈溢出,但都是徒劳的,现在我已经找到了解决方案,这是,如果你正在使用linux,只需安装g编译器从您的终端,
sudo apt安装g
“你可以走了。
0wi1tuuw5#
对于使用WSL的用户,这是一个常见错误,可通过以下方法解决:
Remote-WSL
VS代码扩展c_cpp_properties.json
includePath
设置为["${workspaceFolder}/**"]
,将intelliSenseMode
设置为"linux-clang-x64"
(或其他智能感知模式)现在,
#include
错误应该消失了有关更多信息,请访问the link
kjthegm66#
我也遇到了同样的问题,花了几个小时尝试不同的解决方案,最后,我意识到我在“iostream”中有一个拼写错误。这不是这个问题的答案,但如果你在这里遇到了同样的错误,也要检查拼写。
3phpmpom7#
问题原因:
我的C/C++编译器被保存为
/use/bin/clang
,这在我的计算机中显示了错误。解决方案:
1.打开您的VSCode并单击设置(通常位于左下角)。
1.现在点击打开设置(JSON)图标(在右上角)并打开
json
文件。1.在主
{}
括号 * 的最后一部分之间添加以下代码[如果没有括号,则创建一个]*。这已经解决了我的问题,我认为它将解决任何Linux/Unix操作系统的问题。
如果这不起作用,请尝试:
如果您使用Windows操作系统,则使用愚者编译器的路径而不是
/usr/bin/gcc
[类似于C:\MinGW\bin
]。