Vscode-cpptools: Referencing Arduino files in IntelliSense configuration

Created on 5 Oct 2018  Â·  35Comments  Â·  Source: microsoft/vscode-cpptools

Bug

cannot open source file "avr/pgmspace.h" (dependency of "C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\Arduino.h")

but it compiles ok...
it show this on the Problems tab for each file (.ino) on the project.
it is very annoying.

Extension version: 0.19.0
VS Code version: Code 1.27.2 (f46c4c469d6e6d8c46f268d1553c5dc4b475840f, 2018-09-12T16:17:45.060Z)
OS version: Windows_NT x64 6.1.7601


System Info

|Item|Value|
|---|---|
|CPUs|Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz (8 x 3591)|
|GPU Status|2d_canvas: enabled
checker_imaging: disabled_off
flash_3d: enabled
flash_stage3d: enabled
flash_stage3d_baseline: enabled
gpu_compositing: enabled
multiple_raster_threads: enabled_on
native_gpu_memory_buffers: disabled_software
rasterization: unavailable_software
video_decode: enabled
video_encode: enabled
webgl: enabled
webgl2: enabled|
|Memory (System)|15.90GB (11.02GB free)|
|Process Argv|C:\Users\Arkas_i7AppData\Local\Programs\Microsoft VS Code\Code.exe|
|Screen Reader|no|
|VM|0%|


Language Service more info needed

Most helpful comment

compilerPath = gcc paired with intelliSenseMode = msvc-x64 is not likely to work. Can you set "IntelliSenseMode": "gcc-x64" and try again?

avr/pgmspace.h lives under the \Arduino\hardware\tools\avr folder, which does not appear to be in your includePath. Try adding "C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr\\**" to the includePath

All 35 comments

Please share your c_cpp_properties.json so we can see what the configuration is.

I have exactly the same problem. I have installed MinGW through MSys2, added location of gcc.exe to PATH and then executed "Arduino: Initialize" from Command Palette. It generated ".vscode" folder with files "arduino.json" and "c_cpp_properties.json":
arduino.json:

{
    "sketch": "app.ino",
    "board": "arduino:avr:mega",
    "configuration": "cpu=atmega2560"
}

c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "C:\\Program Files (x86)\\Arduino\\tools\\**",
                "C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\**"
            ],
            "forcedInclude": [
                "C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino\\Arduino.h"
            ],
            "intelliSenseMode": "msvc-x64",
            "compilerPath": "C:\\msys64\\mingw64\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ],
    "version": 4
}

compilerPath = gcc paired with intelliSenseMode = msvc-x64 is not likely to work. Can you set "IntelliSenseMode": "gcc-x64" and try again?

avr/pgmspace.h lives under the \Arduino\hardware\tools\avr folder, which does not appear to be in your includePath. Try adding "C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr\\**" to the includePath

Seconded, I have the exact same c_cpp_properties as above, and am getting the exact same error. Arduino extension version 0.2.24, Arduino IDE Version 1.8.8, VSCode version 1.30.1

@sventec did you try making the changes in my comment?

I am having same problem and have tried gcc.exe as an alternative and also tried @bobbrow suggestion re: the includePath. Every single .ino file throws the same "cannot open source file" error. I have the same installed versions as @sventec. c_cpp_properties.json below:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "C:\\Program Files (x86)\\Arduino\\tools\\**",
                "C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\**",
                "C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr\\**"
            ],
            "forcedInclude": [
                "C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino\\Arduino.h"
            ],
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe",
            "cStandard": "c11",
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}

@mpschawkfan You can enable Debug logging to see which includePath folders we end up using after opening a file. Does the folder containing the "cannot open source file" target appear in the "sending complation args" list?

No, the folder is not there. None of the directories listed under includePath are there...output below:

  include: C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 14.0\VC\INCLUDE
  include: C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 14.0\VC\ATLMFC\INCLUDE
  include: C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.14393.0\UM
  include: C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.14393.0\UCRT
  include: C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.14393.0\SHARED
  include: C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.14393.0\WINRT
  preinclude: C:\PROGRAM FILES (X86)\ARDUINO\HARDWARE\ARDUINO\AVR\CORES\ARDUINO\ARDUINO.H
  other: --header_only_fallback
  stdver: --ms_c++latest
  intelliSenseMode: msvc

The recursive includes code will remove any folders that it detects are not used by the current translation unit. So, I assume your source file doesn't have a #include to the arduino files and instead the #includes are in the forcedInclude, right? There is a bug in that we aren't checking the forced include files for which recursive includes directories to use -- seems like it should be quick/easy to fix.

That's correct. The file that is forcedInclude contains the #include to the .h file that is being reported in Problems as 'cannot open source file'. The problem files are always dependencies of the arduino.h file that is in forcedInclude.

I've filed bug https://github.com/Microsoft/vscode-cpptools/issues/2986 ...not sure if the other users on this thread are hitting the same thing. The current workaround would be to use non-recursive paths for the paths used by the forcedInclude or having the source files #include files in those paths.

@mpschawkfan FYI, the issue has been fixed for our next update...unless the change gets "delayed" for some reason.

Our latest 0.21.0-insiders3 has fixed the issue with recursive includes when forcedInclude is used. Let us know if there is still more problems with the arduino scenario.

Setting C_Cpp.updateChannel to "Insiders" is another way to get the update.

Is this still only fixed in Insiders? Just stumbled upon this with 1.31.1 on Mac.

Edit: I mean, I fixed it by adding "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/**" to the includePath, but I wondered if that was supposed to be unnecessary now.

@ednl The bug that was fixed is https://github.com/Microsoft/vscode-cpptools/issues/2986 , which was when forcedInclude and recursive includes are used. The includePaths for any Arduino header files still needs to be specified somehow, like what you added.

I've fixed "cannot open source file "avr/pgmspace.h"" error by adding <arduino_install_path>\\hardware\\tools\\avr\\avr\\include\\** path to "includePath" in c_cpp_properties.json.

I guess it should be there by default, so it looks like issue of vscode-arduino extension.

I've fixed this issue by appending "C:\\\\Program Files (x86)\\\\Arduino\\\\hardware\\\\tools\\\\**" to the includePath array in .vscode\c_cpp_properties.json.

Had the same issue. Fixed it by updating c_cpp_properties.json file. Added next lines to includePath:

"/Applications/Arduino.app/Contents/Java/hardware/tools/avr/**"
"/Users/oleksiisedun/Documents/Arduino/libraries/**"

And set
"intelliSenseMode": "gcc-x64"

I am sure this should be done automatically by Arduino extension.

Following the instructions above I have found a way to solve the problem, thanks.

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:/Program Files (x86)/Arduino/tools/**",
                "C:/Program Files (x86)/Arduino/hardware/arduino/avr/**",
                "C:/Program Files (x86)/Arduino/hardware/tools/avr/**"
            ],
            "forcedInclude": [
                "C:/Program Files (x86)/Arduino/hardware/arduino/avr/cores/arduino/Arduino.h"
            ],
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ],
    "version": 4
}

Working version for MacOS 10.14.3, VSCode 1.36.1, Arduino extension 1.36.1

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "/Applications/Arduino.app/Contents/Java/tools/**",
                "/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/**",
                "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/**"
            ],
            "forcedInclude": [
                "/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h"
            ],
            "macFrameworkPath": [
                "/System/Library/Frameworks",
                "/Library/Frameworks"
            ],
            "intelliSenseMode": "clang-x64",
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "defines": ["USBCON"]
        }
    ],
    "version": 4
}

Just added line with: "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/**" and "defines": ["USBCON"].

I had the same issue, and after reading this thread and several others I think I've now got it fixed. I think there are 3 key steps:

  1. Fix the include paths
  2. Set the correct compiler path
  3. Remove any other target configurations, eg "Win32"

As stated by @tomajask above, adding "USBCON" to the defines will remove the squiggles for Serial (alternatively you can add the definition for the micro, such as __AVR_ATmega328P__)

So, here is my complete c_cpp_properties.json file for a Windows environment:

{
    "configurations": [
        {
            "name": "Arduino AVR",
            "includePath": [
                "${workspaceFolder}\\**",
                "${env:HOMEPATH}\\Documents\\Arduino\\libraries\\**",
                "C:\\Program Files (x86)\\Arduino\\tools\\**",
                "C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\**",
                "C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr\\lib\\gcc\\avr\\**",
                "C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr\\avr\\include\\**",
                "C:\\Program Files (x86)\\Arduino\\libraries\\**"
            ],
            "defines": [
                "USBCON"
            ],
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr\\bin\\avr-g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++11"
        }
    ],
    "version": 4
}

FWIW, the newest version of vscode (1.37.1) on a clean Windows install was working fine with the default configuration for me, simply editing compiler path/etc to the install locations. This points to my original issue _probably_ being a problem with configs.

            "includePath": [
                "D:\\arduino-nightly\\tools\\**",
                "D:\\arduino-nightly\\hardware\\arduino\\avr\\**",
                "D:\\arduino-nightly\\hardware\\arduino\\**",
                "D:\\arduino-nightly\\hardware\\tools",
                "D:\\arduino-nightly\\hardware\\tools\\avr",
                "D:\\arduino-nightly\\hardware\\tools\\avr\\avr\\include"
            ],

these works fine for me, but it can't find pinmode() and things like this......

it's important to find the files first, isn't it?

I had the same problem, i fixed it by adding "C:\Program Files (x86)\Arduino\hardware\tools\avr\**" in includePath but how can i change the default parameter of includePath so i will not have to change it every time i create a project?

@rec0gn1ze You could set the C_Cpp.default.includePath setting and then new projects will pick that that by default, or if new project need additional includes you can pull the default in via using "${default}" in your includePath.

Had the same issue and after digging around for a while, this thread saved me! Here's what my configuration looks like:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "/home/thegeekylad/Downloads/arduino-1.8.9/tools/**",
                "/home/thegeekylad/Downloads/arduino-1.8.9/hardware/arduino/avr/**",
                "/home/thegeekylad/Downloads/arduino-1.8.9/hardware/tools/avr/**",
                "${workspaceFolder}"
            ],
            "forcedInclude": [
                "/home/thegeekylad/Downloads/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Arduino.h"
            ],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

TL;DR

adding "/home/thegeekylad/Downloads/arduino-1.8.9/hardware/tools/avr/**" to includePath sorted it all out irrespective of intelliSenseMode.

Setting this up looks like black magic. Every solution is like a magic potion which works for some and doesn't work for others :/

So here's my fix: intellisense stopped complaining after adding the following lines to includePath:

    "C:/Program Files (x86)/Arduino/**",
    "C:/Users/knee-/Documents/Arduino/libraries/**"

So the includePath now looks like this:

  "includePath": [
    "${workspaceFolder}/**",
    "C:/Program Files (x86)/Arduino/**",
    "C:/Users/knee-cola/Documents/Arduino/libraries/**"
  ],

@knee-cola this solved the custom library issue.

But still i get the following error.

"cannot open source file \"avr/pgmspace.h\"

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "C:\\Program Files (x86)\\Arduino\\tools\\**",
                "C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\**",
                "C:\\Users\\Manjunath CV\\Documents\\Arduino\\libraries\\**"

            ],
            "forcedInclude": [
                "C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino\\Arduino.h"
            ],
            "intelliSenseMode": "msvc-x64",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/Hostx64/x64/cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ],
    "version": 4
}

@cvmanjoo Do you need to add C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\ to includePath? Or wherever "avr/pgmspace.h" is located?

@sean-mcmanus

The path of avr/pgmspace.h is
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr\\avr\\include\\avr\\**" even adding this didnt help.

Am I missing something?

I've stopped using the "forcedInclude" config option, and add #include <Arduino.h> in the source where appropriate (pay attention to the uppercase "A").

I listed my config earlier but I've since tweaked it a little. This is using environment variables so with any luck you can simply copy & paste. I have tailored this for the Mega 2560 however, and I have similar configs defined for ESP8266 & ESP32 (different compiler path and defines).

Hope this helps someone.

{
    "configurations": [
        {
            "name": "AVR Mega 2560",
            "includePath": [
                "${workspaceFolder}\\**",
                "${env:HOMEPATH}\\Documents\\Arduino\\libraries\\**",
                "${env:USERPROFILE}\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.2\\cores\\arduino\\**",
                "${env:USERPROFILE}\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.2\\variants\\mega\\**",
                "${env:USERPROFILE}\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.2\\libraries\\**"
            ],
            "defines": [
                "ARDUINO_ARCH_AVR",
                "USBCON"
            ],
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "${env:USERPROFILE}\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino5\\bin\\avr-g++.exe -mmcu=atmega2560",
            "cStandard": "c11",
            "cppStandard": "c++11"
        }
    ],
    "version": 4
}

This works for me:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/<user_path>/arduino/arduino-1.8.12/**",
                "/<user_path>/arduino/arduino-1.8.12/tools/**",
                "/<user_path>/arduino/arduino-1.8.12/libraries/**",
                "/<user_path>/arduino/arduino-1.8.12/hardware/arduino/avr/**"
            ],
            "forcedInclude": [
                "/<user_path>/arduino/arduino-1.8.12/hardware/arduino/avr/cores/arduino/Arduino.h"
            ],
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "gnu++14"
        }
    ],
    "version": 4
}

It seems that most folks have figured out their Arduino config issues. I'm going to close this for now. If you continue to have problems, please open a new issue and let us know what remains to be solved.

Thank you!

Maybe you should create one central issue and direct all similar open and closed issues to it until this issue gets fixed. I can't understand closing an issue when the bug hasn't been fixed yet.

btw, I followed the recommendations here and got my install working.

@rshpeley, what is the bug that isn't fixed? It sounds like you were able to configure your project.

Was this page helpful?
0 / 5 - 0 ratings