首页 > 解决方案 > 在 VS Code 中使用 Arduino 时,IntelliSense 会引发 #include 错误

问题描述

我想在 VS Code 中开发 Arduino 代码。因此,我安装了 Arduino IDE 和 Arduino 扩展 vor VS Code。

在 VS Code 中打开 Arduino 项目后,扩展c_cpp_properties.json为 IntelliSense 创建了以下文件(摘录):

"includePath": [
        "/Applications/Arduino.app/Contents/Java/tools/**",
        "/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/**"
],
"forcedInclude": [
        "/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h"
],

强制包含Arduino.h具有以下相对包含:

// Some includes
#include <avr/pgmspace.h>
// Even more includes

问题是,尽管pgmspace.h确实存在,但它并不位于相对于的路径中Arduino.h(它也不位于两个包含路径之一中)。将路径添加pgmspace.h到包含路径并没有帮助,因为 IntelliSense 似乎正在寻找给定的相对路径。

我的问题是是否有可能通过c_cpp_properties.json文件告诉 IntelliSense 忽略相对路径并只查找文件?或者如果您能想出另一种方法来解决这个问题?

标签: c++visual-studio-codearduinoincludeintellisense

解决方案


我自己找到了答案。如果您有同样的问题,请尝试以下操作:

添加到settings.json

"C_Cpp.intelliSenseEngine": "Tag Parser"

这是一个缺点,因为“标签解析器”不是上下文感知的。

添加到c_cpp_properties.json

"configurations": [
  {

    ...,

    "browse": {
      "path": [
        "/Applications/Arduino.app/Contents/Java/tools",
        "/Applications/Arduino.app/Contents/Java/hardware/arduino/avr",
        "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include"
      ],
      "limitSymbolsToIncludedHeaders": true
    }
  }
]

推荐阅读