VS Code errors (with library paths I think?)

Obviously not an expert at this stuff...

I'm trying to get VS Code set up for Arduino (on Windows 10). Right now, I can upload basic samples via VSC and they work fine, but I have problems with sketches I developed in the Arduino IDE.

I believe it's around the library paths but I'm not sure. I'm trying to use the FastLED library and my first include line (for FastLED.h) gets a red underline. The errors are:

cannot open source file "avr/io.h" (dependency of "FastLED.h")C/C++(1696)
#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit (C:\Users****\Documents\Arduino\project1\project1.ino).C/C++(1696)
cannot open source file "avr/pgmspace.h" (dependency of "C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\Arduino.h")C/C++(1696)

I've found the "avr/io.h" file in the C:\Program Files (x86)\Arduino\hardware\tools... folder and tried adding it to the c_cpp_properties.json file but I'm not having any luck. (I only tried that after some internet searches led me down that path).

Everything works fine via the Arduino IDE.

Can anyone help me out? I can provide more info, but I'm not sure what's relevant...

Thanks!

Are you using the Microsoft provided plugin for Arduino? If so, it is a thin wrapper on the Arduino command line, calling pretty much the same operations as the standard IDE.

Is the issue a compile error where the sketch fails to build or intelli-sense error in the editor?

You may have already done this but: When you load your own sketch into VS code you need to select ‘Arduino initialise’ from the CTRL-p menu. This creates an Arduino configuration in the sketch directory, and in that file there’s configuration for include paths etc.

1 Like

Although saying that it uses the same operations, I have noticed changes in its behaviour since I upgraded to the latest Arduino IDE version recently. I now get a lot of Java logging I didn’t used to get in the build console.

But still, I’ve not had a sketch that would build in Arduino IDE not build in VS Code. I’ve got quite a few where I can’t get the intelli-sense fully working.

Those look like intellisense error messages from vscode. You likely have intellisense configuration issues. If the code compiles using the IDE then it will still compile when you use the vscode plugin since the plugin uses the ide to do the compile. You can optionally configure vscode build tasks and use the arduino cli.

If you want vscode to properly show the errors so you need to configure the vscode intellisense c_cpp_properties.json. It is important to understand that this file needs to be configured with the include paths and the proper build defines so that vscode lints the code properly.

If you are missing an include path, vscode cannot find it because it doesn't know the paths to the include files unless it is specified in the include paths. So, every include statement in your source files must have an include path in c_cpp_properties.json. I have found that recursive paths does not work for the arduino libraries folder for some reason. So, you need to specify each path individually. Here is a sample:

            "includePath": [
                "${workspaceRoot}/src/",
                "/Users/stevenslupsky/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/**",
                "/Users/stevenslupsky/Library/Arduino15/packages/arduino/tools/CMSIS/4.5.0/CMSIS/Include/**",
                "/Users/stevenslupsky/Library/Arduino15/packages/arduino/tools/CMSIS-Atmel/1.2.0/CMSIS/Device/ATMEL/**",
                "/Users/stevenslupsky/Library/Arduino15/packages/arduino/tools/CMSIS-Atmel/1.2.0/CMSIS/Device/ATMEL/samd21/**",
                "/Users/stevenslupsky/Library/Arduino15/packages/arduino/hardware/samd/1.8.2/cores/arduino/",
                "/Users/stevenslupsky/Library/Arduino15/packages/arduino/hardware/samd/1.8.2/variants/mkrwan1300/",
                "/Users/stevenslupsky/Library/Arduino15/packages/arduino/hardware/samd/1.8.2/libraries/**",
                "/Users/stevenslupsky/Library/Mobile Documents/com~apple~CloudDocs/Arduino/libraries/RTCZero/src/",
                "/Users/stevenslupsky/Library/Mobile Documents/com~apple~CloudDocs/Arduino/libraries/Queue/src",
                "/Users/stevenslupsky/Library/Mobile Documents/com~apple~CloudDocs/Arduino/libraries/jled/src/",
                "/Users/stevenslupsky/Library/Mobile Documents/com~apple~CloudDocs/Arduino/libraries/SimplyAtomic/",
                "/Users/stevenslupsky/Library/Mobile Documents/com~apple~CloudDocs/Arduino/libraries/Embedded_RingBuf_CPP/src/",
                "/Users/stevenslupsky/Library/Mobile Documents/com~apple~CloudDocs/Arduino/libraries/witap/src/",
                "/Users/stevenslupsky/Library/Mobile Documents/com~apple~CloudDocs/Arduino/libraries/Arduino-CommandLine/",
                "/Users/stevenslupsky/Library/Mobile Documents/com~apple~CloudDocs/Arduino/libraries/ArduinoLog/",
                "/Users/stevenslupsky/Library/Mobile Documents/com~apple~CloudDocs/Arduino/libraries/ArduinoLowPower/src/",
                "/Users/stevenslupsky/Library/Mobile Documents/com~apple~CloudDocs/Arduino/libraries/arduino-ZeroRegs/src/",
                "/Users/stevenslupsky/Library/Arduino15/packages/arduino/hardware/samd/1.8.2/**",
                "/Users/stevenslupsky/Library/Arduino15/packages/arduino/tools/**"
            ],

Equally important is the linting requires that the defines for your build environment are properly specified as well. Here is a sample of the defines I use for a MKR WAN board:

            "defines": [
                "__CC_ARM",
                "F_CPU=48000000L",
                "ARDUINO=10607",
                "ARDUINO_SAMD_MKRWAN1300",
                "ARDUINO_ARCH_SAMD",
                "USE_ARDUINO_MKR_PIN_LAYOUT",
                "__SAMD21G18A__",
                "USB_VID=0x2341",
                "USB_PID=0x8053",
                "USBCON",
                "USE_BQ24195L_PMIC",
                "VERY_LOW_POWER"
            ],

You should also force include "Arduino.h":

            "forcedInclude": [
                "/Users/stevenslupsky/Library/Arduino15/packages/arduino/hardware/samd/1.8.2/cores/arduino/Arduino.h"
            ],

And finally, use the arm compiler for linting as follows:

            "intelliSenseMode": "gcc-x64",
            "compilerPath": "/Users/stevenslupsky/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/bin/arm-none-eabi-g++",
            "cStandard": "c11",
            "cppStandard": "c++11"

Generally speaking, this configuration works pretty well for me.

Many Thanks @sslupsky that’s very helpful. I’m running on Windows and kind of put up with a few intelli-sense problems for a while.

To be honest I use VS with visual micro for heavier development. But VS code is really good for testing libraries because it’s really easy to switch between examples and tests.

I usually get the header paths right but have never tried the compiler options before. I’ll give it a try.

davetcc:
...You may have already done this but: When you load your own sketch into VS code you need to select ‘Arduino initialise’ from the CTRL-p menu. This creates an Arduino configuration in the sketch directory, and in that file there’s configuration for include paths etc.

Thank you!! I guess I need to learn more about VSC (or programming in general?). I was only working with a "file" and I guess it was trying to use a global json file? Anyway, I did an "open folder" instead of "open file", then did the "initialize" (which I had no idea that I had to do), and now everything works (and I have a .vscode folder in my project folder too).

Thanks again, I was really getting discouraged, but now I'm moving forward again!!!