ar-std
ar-std

Reputation: 111

IntelliSense throws #include errors when working with Arduino in VS Code

I want to develop Arduino code in VS Code. Therefore, I installed the Arduino IDE and the Arduino extension vor VS Code.

Upon opening an Arduino project in VS Code the extension created the following c_cpp_properties.json file for IntelliSense (excerpt):

"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"
],

The forced include Arduino.h has the following relative include:

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

The problem is that, although pgmspace.h does exist it is not located in the path relative to Arduino.h (it is not located in one of the two include paths either). Adding the path of pgmspace.h to the include paths does not help as IntelliSense seems to be looking for the given relative path.

My question is if there is any possibility to tell IntelliSense via the c_cpp_properties.json file to ignore relative paths and just look for the file? Or if you can think of another way to resolve this problem?

Upvotes: 4

Views: 5625

Answers (3)

Idan Gozlan
Idan Gozlan

Reputation: 3203

On macOS, modify your .vscode/c_cpp_properties.json file with the following:

"includePath": [
   ...,
   "/Users/YOURUSERNAME/Documents/Arduino/libraries/**"
]

Upvotes: 1

Arduino.h is located in /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h and it has the following snippet:

#include <avr/pgmspace.h>
#include <avr/io.h>
#include <avr/interrupt.h>

E.g. the first file pgmspace.h is in /Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/avr/pgmspace.h (which you can find for example with the shell command "find / -name pgmspace.h"), and that path cannot be found as <avr/pgmspace.h> relative to where Arduino.h is, of course.

But it can be found relative to /Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include. Add that to your c_cpp_properties.json, and VSCode should be able to find the header files.

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

Upvotes: 3

ar-std
ar-std

Reputation: 111

I found an answer myself. If you have the same problem, try the following:

Add to settings.json:

"C_Cpp.intelliSenseEngine": "Tag Parser"

This is a downside as "Tag Parser" is not context-aware.

Add to 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
    }
  }
]

Upvotes: 7

Related Questions