11

I've recently started developing for Arduino. Initially I used the Arduino IDE but I soon realised it was not up to par. Development was considerably slower with the need for IDE restarts every now and then. I found VS Code with the Arduino Extension which I've come to love. However I have a few issues and I'm not sure how to solve it.

First of all vs code throws #include errors and asks me to update IntelliSense. However it builds/uploads and runs perfectly well, it also finds the classes etc. defined in said includes so it looks like it's a false positive in some way (i.e. the path is included in includePath settings). Reading the error message also shows it having issues finding a header referenced in Arduino.h called "avr/pgmspace.h". I'm unsure if these errors are related. pgmspace.h is nowhere to be found (it should have been included in the Arduino SDK).

Finally because of the #include error anything related to that particular header file will not be highlighted properly and is just plain gray text which is a bit annoying.

Anyone knows how to fix this? I'm on a Mac btw.

Error messages

Gama11
  • 31,714
  • 9
  • 78
  • 100
sebrock
  • 1,214
  • 2
  • 15
  • 32

5 Answers5

15

Update: Extra libraries do not need to be installed. IntelliSense can operate using only the headers installed by the Arduino app, but a few others may help. More updates below.

When VSCode builds, it uses the SDK. However, IntelliSense cannot read the SDK files to operate (as far as I can tell), which throws these annoying errors and eliminates most code completion capabilities.

Both includePath and browse.path need to be configured. includePath does not include recursively (but that feature seems to be coming soon). browse.path is recursive, but including the exact location of header files is in includePath is still necessary for IntelliSense features. browse.path will use the Tag Parser to provide tools such as the "lightbulb" that you can click to help resolve your includePath issue. (Source: What is the difference between "includePath" and "browse.path" in c_cpp_properties.json?)


Mac:

avr/pgmspace.h is located at: /Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/avr/pgmspace.h. It's coded into the libraries as avr/pgmspace.h; for this reason, we need to include the path that avris located in.

1. Install homebrew-avr:

2. Edit c_cpp_properties.json:

"includePath": [
    "${workspaceFolder}/libraries",
    "/System/Library/Frameworks/Kernel.framework/Versions/A/Headers",
    "/Applications/Arduino.app/Contents/Java/libraries",
    "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include",
    "/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/variants/standard"
],
"browse": {
    "limitSymbolsToIncludedHeaders": false,
    "path": [
        "/System/Library/Frameworks/Kernel.framework/Versions/A/Headers",
        "/Applications/Arduino.app/Contents/Java/"
    ]
},
"forcedInclude": [
    "/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h"
],

Windows 10:

1. Install WinAVR

2. Edit c_cpp_properties.json:

Revise includePath to look like this:

"includePath": [
    "{$workspaceFolder}/libraries",
    "C:/Program Files (x86)/Arduino/hardware/tools/avr/lib/gcc/avr/5.4.0/include",
    "C:/Program Files (x86)/Arduino/hardware/arduino/avr/cores/arduino",
    "C:/Program Files (x86)/Arduino/hardware/arduino/avr/variants/standard",
    "C:/Program Files (x86)/Arduino",
],

Alternative Method (probably a bad idea):

If you can get a version of any missing library, you can probably put in in your libraries folder of your project if your includePath includes "${workspaceFolder}/libraries". Make sure to namespace your libraries appropriately, e.g. libraries/avr/pgmspace.h. I have not tested this method.


Updates:

  • Changed from home.path to includePath. See more here: vscode-cpptools/FAQ
  • The tools downloaded by the vscode-arduino libraries are not necessary on Windows 10.
  • Changed Windows config paths to use forward slashes instead of escaped backslashes.

Source and further tips: Enabling Arduino Intellisense with Visual Studio Code

Joe Sadoski
  • 586
  • 2
  • 7
  • 22
  • Unfortunately this didn't work for me. It seems to be a bug tbh as it doesnt seem to respect the includePaths. I'm going to post a bug on Github and report back if it gets resolved. – sebrock Sep 17 '18 at 08:26
  • Please share that issue thread here, if you can. I would be interested to know as well. – Joe Sadoski Sep 18 '18 at 21:35
  • So I got it working. Unfortunately I'm not sure exactly what made it work. It was probably a number of things. I'm leaning towards suspecting that there was a clash in multiple intellisense engines working at once. My instance of VS Code had at two Intellisense extensions installed. Now I'm only running the official C/C++ one and it seems to do the trick. I'm gonna mark your answer correct anyway. – sebrock Sep 27 '18 at 16:44
  • Thanks, and good to know. As I've moved on to working with other libraries, I've repeatedly run into issues that don't make sense. Yesterday, IntelliSense threw errors for a .io in one project and not the other, and I had copy-pasted the entire c_cpp_properties.json and fixed small details regarding relative file paths. My instantiation of Serial() said it had no declaration, but I could right-click and use Go To/Peek definition that took me to the header. The whole system seems like it needs improvement. I would also appreciate if Arduino.cc had better documentation about their libraries. – Joe Sadoski Sep 28 '18 at 17:15
6

The accepted answer didn't work for me. Can't find nor create c_cpp_properties.json file. Also, I wanted it to be global, and not only to one project/workspace/folder.

So, for VSCode 1.14 (2019) I just navigate till settings.json (the global one), and add this json section:

"C_Cpp.default.includePath": [
    "C:/Program Files (x86)/Arduino/libraries/**",
    "C:/Program Files (x86)/Arduino/hardware/arduino/avr/cores/arduino/**",
    "C:/Program Files (x86)/Arduino/hardware/tools/avr/avr/include/**",
    "C:/Program Files (x86)/Arduino/hardware/tools/avr/lib/gcc/avr/5.4.0/include/**",
    "C:/Program Files (x86)/Arduino/hardware/arduino/avr/variants/standard/**",
    "C:/Users/<YOUR USERNAME>/.platformio/packages/framework-arduinoavr/**",
    "C:/Users/<YOUR USERNAME>/Documents/Arduino/libraries/**",
    "{$workspaceFolder}/libraries/**",
    "{$workspaceFolder}/**"
],
"C_Cpp.intelliSenseEngine": "Tag Parser"

Posted another answer with the entire procedure and all details about the approach: Visual Studio Code includePath

Vitox
  • 3,852
  • 29
  • 30
  • Not sure how you ”can not create” the file. It’s your file system :) but glad you got it working. – sebrock Nov 13 '19 at 18:13
  • @sebrock I mean that i can't find any option to create that automatically, as other solutions (not in this question) suggests. There is a tutorial that says: "click on the error. That will create the c_cpp_properties.json file for you. I simple could not find that option anywhere, also didn't know where to create that file, nor what format/structure it should be (what properties, values, etc...) – Vitox Nov 13 '19 at 22:22
  • @sebrock so... sure, i could create a .json file anywhere hehe. But I didn't and still do not have any idea where to create that, and what properties and values it should have. Anyway, my solutions seems lot easier, and gets the job done. : ) – Vitox Nov 13 '19 at 22:25
1

I am using VSC for code editor, and need only intellisense. I set in the arduino IDE preferences "use external editor" and I using it for compile and upload.

My win username "bunny" and the Sketchbook location is "C:\person\Arduino" for understand the folders. I am using latest VSC (1.56.2) and ArduinoIDE (1.8.13)

My settings.json like this, there is working well with all library included special like esp8266 etc:

{
"C_Cpp.intelliSenseEngine": "Tag Parser",
    "C_Cpp.default.browse.path": [
        "c:/Person/Arduino/libraries",
        "c:/Person/Arduino/hardware",
        "c:/Program Files (x86)/Arduino/hardware",
        "c:/Program Files (x86)/Arduino/libraries",
        "c:/Users/Bunny/AppData/Local/Arduino15/packages",
        "${workspaceFolder}"
    ]
}
bunnyhu
  • 21
  • 3
  • None of the other answers seemed to work, but this one did (VSC 1.70.0, Arduino 1.8.13). I only needed to include "${workspaceFolder}", as vs code (IntelliSense) was only confused about include files in my project. Thanks! – Chris Torrence Aug 21 '22 at 15:35
1

The accepted answer is very good. I had to include some other folders as well, my includePath looks like this and I have no IntelliSense erros any more:

"includePath": [
                "${workspaceFolder}/**",
                "C:/Program Files (x86)/Arduino/hardware/tools/avr/lib/gcc/avr/7.3.0/include",
                "C:/Program Files (x86)/Arduino/hardware/arduino/avr/cores/arduino",
                "C:/Program Files (x86)/Arduino/hardware/arduino/avr/variants/standard",
                "C:/Users/<USER_NAME>/Documents/Arduino/libraries/**",
                "C:/Users/<USER_NAME>/AppData/Local/Arduino15/packages/arduino/hardware/avr/1.8.5/libraries/**",
                "C:/Users/<USER_NAME>/AppData/Local/Arduino15/libraries/**"
            ],

You can use these paths as inspiration to add if you encounter some header that cannot be found (like SPI.h or Ethernet.h).

Stefan
  • 919
  • 2
  • 13
  • 24
0

The mentioned c_cpp_properties.json is located inside the project's .vscode directory.

If not present you can create it in VSCode by pressing Ctrl+Shift+P and typing the C/C++: Edit Configurations (JSON) command.

To improve upon Stefan's answer you can use ${env:USERNAME} in replacement of <USER_NAME> to make it automatically replaced by the current user's name.

These worked out for me:

"includePath": [
    "${workspaceFolder}/**",
    "C:/Program Files (x86)/Arduino/**",
    "C:/Program Files (x86)/Arduino/hardware/tools/avr/lib/gcc/avr/7.3.0/include",
    "C:/Program Files (x86)/Arduino/hardware/arduino/avr/cores/arduino",
    "C:/Program Files (x86)/Arduino/hardware/arduino/avr/variants/standard",
    "C:/Users/${env:USERNAME}/Documents/Arduino/libraries/**",
    "C:/Users/${env:USERNAME}/AppData/Local/Arduino15/packages/**",
    "C:/Users/${env:USERNAME}/Documents/Arduino/libraries/Adafruit_GFX_Library/",
    "C:/Program Files (x86)/Arduino/hardware/arduino/avr/libraries/SPI/src"         
],

This was also useful, to change the compiler to gcc as naigonakoii suggests here:

"compilerPath": "C:/Program Files (x86)/Arduino/hardware/tools/avr/bin/avr-gcc.exe",
"intelliSenseMode": "gcc-x86",

And by adding some defines we have the ultimate anti-squiggly-line configuration (You might need to change the last one for the model you're using

"defines": [
    "_DEBUG",
    "UNICODE",
    "_UNICODE",
    "ARDUINO=100",
    "USBCON",
    "__AVR_ATmega328P__"
],
2bam
  • 83
  • 6