How to make Arduino work and not give "cannot open source file "avr/pgmspace.h"" on vscode?
Asked Answered
C

3

7

I'm trying to program arduino in vscode. The problem is that It's giving me weird header errors: cannot open source file "avr/pgmspace.h" (dependency of "C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\Arduino.h")

This is my arduino.json:

    "board": "arduino:avr:uno"
}

This is my 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:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ],
    "version": 4
}

It should recursively include all of the required libraries, and even if imanually try to add the path to avr/pgmspace.h or its directory, it keeps giving me the same errors.

How do I solve this error?

Changeover answered 28/1, 2019 at 9:21 Comment(0)
F
8

Based on the responses to this issue, I added "c:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr\\avr\\**" to my include path in c_cpp_properties.json:

"includePath": [
    "c:\\Program Files (x86)\\Arduino\\tools\\**",
    "c:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\**",
    "c:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr\\avr\\**"
],
Filmy answered 4/1, 2020 at 20:46 Comment(0)
T
6

For anyone who stumbles here on a Mac, here is the config that works for me with the Uno:

{
    "env": {
        "arduino_path": "/Applications/Arduino.app/Contents/Java",
        "arduino_avr_include_path": "${env:arduino_path}/hardware/arduino/avr",
        "arduino_avr_include2_path": "${env:arduino_path}/hardware/tools/avr/avr/include",
        "arduino_avr_compiler_path": "${env:arduino_path}/hardware/tools/avr/bin/avr-g++"
    },
    "configurations": [
        {
            "name": "Mac",
            "defines": [
                "ARDUINO=10810",
                "__AVR_ATmega328P__",
                "UBRRH"
            ],
            "includePath": [
                "${workspaceRoot}",
                "${env:arduino_avr_include_path}/**",
                "${env:arduino_avr_include2_path}/**"
            ],
            "forcedInclude": [
                "/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h"
            ],
            "intelliSenseMode": "gcc-x64",
            "cStandard": "c11",
            "cppStandard": "c++11",
            "compilerPath": "${env:arduino_compiler_path} -std=gnu++11 -mmcu=atmega328p"
        }
    ],
    "version": 4
}

The key to finding <avr/pgmspace.h> was the adding the hardware/tools/avr/avr/include path.

Defining ARDUINO=10810 was identified from the output of running Arduino: Verify with the verbose flag.

Defining __AVR_ATmega328P__ was added to allow proper IntelliSense completion of the raw register macros (e.g. _BV(), OCR0A, TIMSK0, etc.); the correct macro to define was identified based on the chip being an ATMEGA328P-PU and by inspection of the file hardware/tools/avr/avr/include/avr/io.h.

Tungsten answered 25/1, 2020 at 23:44 Comment(2)
Thank you. "AVR_ATmega328P" helped me a lot and has fixed my troubles with _BV(), OCR0A, TIMSK0, etc.Nullify
It was early to celebrate. When I've add these lines into my c_cpp_properties.json then after running verification something rewrited this file... Is it normal behavior? Or what should I do to prevent rewriting my changes in c_cpp_properties.json?Nullify
B
3

The compilerPath value looks wrong, although it is only used by the IDE and not for target compilation. The documentation says :

The absolute path to the compiler you use to build your project. The extension will query the compiler to determine the system include paths and default defines to use for IntelliSense.

In any case I recommend to configure it properly, I was able to remove

"${HOME}/.arduino15/packages/arduino/tools/avr-gcc/5.4.0-atmel3.6.1-arduino2/bin/../lib/gcc/avr/5.4.0/include",
"${HOME}/.arduino15/packages/arduino/tools/avr-gcc/5.4.0-atmel3.6.1-arduino2/bin/../lib/gcc/avr/5.4.0/include-fixed",
"${HOME}/.arduino15/packages/arduino/tools/avr-gcc/5.4.0-atmel3.6.1-arduino2/bin/../lib/gcc/avr/5.4.0/../../../../avr/include"

when setting

"compilerPath": "${HOME}/.arduino15/packages/arduino/tools/avr-gcc/5.4.0-atmel3.6.1-arduino2/bin/avr-g++ -std=gnu++11 -mmcu=atmega328p",

To figure out the exact compiler that is used turn on verbose logging in the output window:

File -> Preferences -> Settings -> Extensions -> Arduino configuration -> Log level -> verbose

which in this case also should help you figure out why the compiler does not pick up avr/pgmspace.h.


Here are my arduino.json

{
    "board": "arduino:avr:uno",
    "port": "/dev/ttyUSB0",
    "sketch": "src/myproject.ino",
    "output": "../build"
}

and c_cpp_properties.json

{
    "env": {
        "arduino.path": "${HOME}/.arduino15/packages/arduino",
        "arduino.avr.include.path": "${env:arduino.path}/hardware/avr/1.6.23",
        "arduino.avr.compiler.path": "${env:arduino.path}/tools/avr-gcc/5.4.0-atmel3.6.1-arduino2/bin/avr-g++",
        "arduino.libraries.path": "${HOME}/arduino/sketchbook/libraries",
        "dummy-last-line": "To allow the second to last line (e.g. the real last line) to always end with a comma"
    },
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "./src",
                "./test",
                "../arduino_ci/cpp/unittest",
                "${env:arduino.libraries.path}/SmartLCD",
                "${env:arduino.libraries.path}/Chronos/src",
                "${env:arduino.libraries.path}/Time",
                "${env:arduino.libraries.path}/RTClib",
                "${env:arduino.avr.include.path}/libraries/Wire/src",
                "${env:arduino.avr.include.path}/cores/arduino",
                "${env:arduino.avr.include.path}/variants/standard"
            ],
            "browse": {
                "path": [
                    "${workspaceFolder}/src"
                ],
                "limitSymbolsToIncludedHeaders": true
            },
            "defines": [
                "UBRRH"
            ],
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "${env:arduino.avr.compiler.path} -std=gnu++11 -mmcu=atmega328p",
            "cStandard": "c11",
            "cppStandard": "c++11"
        }
    ],
    "version": 4
}

(the UBRRH define is for the Serial variable in HardwareSerial.h)

Briggs answered 9/2, 2019 at 11:2 Comment(1)
I'm using windows so It's very difficult to me to use all of the linux paths you have.Changeover

© 2022 - 2024 — McMap. All rights reserved.