0

So I started typing out this question, but in the process of taking screenshots and looking up sources for the question I figured out the answer :P

That said, this problem has been a snag for me for a while and I've asked multiple people about it with no solution. Because it seemed kind of daunting for me and I haven't seen many well explained reasons I figured I'd finish out the post here to help anyone that is running into this or will run into it in the future.


The snag

I'm using vscode for my editor and I've had this snag for a while but it's finally driving me nuts enough to ask for help.

When in include certain libraries I get error squiggles under the include.

squiggle

In this case the squiggle is popping up because of the error:

cannot open source file "WProgram.h" (dependency of "Adafruit_MPR121.h")C/C++(1696)

WProgram.h error

At first I though the snag was because my include path needed my arduino libraries directory so I added that for the project, but I still get the snag.

updating the include path

I did a computer wide search for the WProgram.h file but was not able to find it.

Unable to find header file

The thing is, all of my sketches still compile and upload fine so for the most part I'm just able to ignore it ... but the squiggle!!!!!!.

Any ideas of how I can resolve this?

4

I landed here in the same position as you, and after researching the comments mentioning -DARDUINO=10813 and some more googling I found: https://github.com/microsoft/vscode-arduino/issues/1148

This led me to adding one more line to defines in my c_cpp_properties.json

and this also removed the squiggly!

{
    "configurations": [
        {

            ...

            "defines": [
                "USBCON",
                "ARDUINO=10813"
            ],

            ...

        }
    ],

}```
1
  • Just because this doesn't answer why this works: Arduino versions <1.0 used WProgram.h, but Arduino >=1.0 use Arduino.h. As @Chris Schmitz explained, many libs use a check to determine which to load for backwards compatibility. Because the VSCode extension should be up to date, we can use the (current) latest version of 1.8.13, or 10813 as it's defined in the code. Adding this to VSCode lets packages checking versions see we're running modern versions. – IronSean Mar 5 at 16:35
2

The answer

Like I said in the question, I found the answer after doing some searching while typing up the question itself and I want to explain it for future squiggle-haters :)

WProgram.h vs Arduino.h

I did a bunch of searching to try to track down what's going on and I eventually found this helpful (and depending on the veracity of the source, kind of sad) bit of history:

There is history of the arduino project not everyone was aware of or supposed to know. I only this much: this all started with some graduate student(s) that created the wiring platform and IDE, called wiring board and wiring "programming language". This project is still active with active users like the arduino project. Then the arduino folks decide to create the arduino project based on wiring project so they took the wiring IDE (which took from the original Processing IDE), including all the libraries etc. and created the first Arduino board (slightly different than wiring board, cheaper!) called it arduino. Arduino project has not very positively acknowledged the wiring project as the source of their project and wiring folks didn't take it very well.

Now, that WProgram.h, is a definition file created by and for the wiring project and board. That's why it has a W on it. It has been in Arduino IDE since its beginning and was replaced by Arduino.h only recently when Arduino 1.0 was released. Now all Arduino 1.0+ IDE will use Arduino.h and not WProgram.h so if you have that plus some other old stuff, you will have to make changes to work with Arduino 1.0+ IDE.

The temp fix

With that in mind I dove into the source file for the Adafruit_MPR121.h library and found where it includes the WProgram.h header file and commented it out.

adjusting the source code

It's also worth pointing out that the pre-compiler conditional here supports the renaming of the file in newer versions of Arduino.

pre-compiler conditional

I labeled this section of the answer as "The temp fix" because it involves modifying a 3rd party dependency which is normally not a great idea. Yeah it fixes it now, but as soon as I need to update this library that edit is going to get blown away and I'd need to manually do it again. In this case the update is pretty minimal so maybe that's no big deal, but if the fix was more involved, like a heavy edit, that would be a real big pain in the butt and could cause other unforeseen consequences (and I played half-life as a kid so I know that's not good).

The better fix

Normally I would say the better fix for this would be to clone the source repository, make the fix there, and do a pull request so the dependency itself gets updated at the source (if someone approves and merges the PR).

But this is a bit different, the conditional in there is actually fine as it adds backwards compatibility. It's there so that the library can support both pre-1.0 and post-1.0 arduino IDE versions. If we did a PR to cut out the WProgram.h reference we'd add in breaking code for anyone on the older versions.

So, the better solution is to appease my local compiler.

We know that the Arduino.h header for all intents and purposes is the same as the WProgram.h, so a almost-full-proof solution would be to fake out the WProgram.h file on my computer to appease the compiler.

Faking WProgram.h

An important part to note here is that to make sure I faked it out correctly I went to the arduino repository, scrolled back through the tags, found the last tag before they switched to 1.0+, and found the WProgram.h file. Once there I grabbed the same pre-compiler check and definition for the WProgram header so I could be sure I did the correct define for the WProgram name.

Correct WProgram define string

This feels like a bit of overkill because it protects me from other headers being included with the same string defined, but if that happened it would be because I have the WProgram file anyway, but I figured if I was going to fake it out I'd do it right.

This could backfire if I include a library that only includes WProgram.h and not the Arduino.h in that arduino version check conditional, but this would 100% be the case for a correction and pull request for that library.

Summary

So now I know that the Arduino.h file is the reason I'm able to compile everything correctly even with the red squiggle, I have a faked out WProgram.h so that my compiler can find something when looking for the file and, most importantly, I no longer need to look at those red squiggles under the includes :)

no squiggles!

Next step, figure out the same thing for Sam.h :P

8
  • 1
    The simplest fix: create WProgram.h and place in it #include <Arduino.h>. – Majenko Nov 27 '20 at 14:57
  • Ah, that's a good point, though I feel like I'd run into the same modifying-code-that-could-be-overwritten issue if I put it in Arduino.h and then an update to the arduino IDE modifies that file. – Chris Schmitz Nov 27 '20 at 14:59
  • 2
    platformio doesn't define Arduino version? you can add -DARDUINO=150 somewhere in projects properties? – Juraj Nov 27 '20 at 15:08
  • 2
    If you want to be compatible with the Arduino ecosystem, the proper fix would be to use the same preprocessor macros as the Arduino IDE itself. For instance, Arduino 1.8.13 compiles every file with the command-line option -DARDUINO=10813. – Edgar Bonet Nov 27 '20 at 15:12
  • Oh interesting, I didn't know about preprocessor macros! I did some searches through the arduino docs just now but can't find where it's talked about. Could someone point me to the right spot in the docs? If I figure out how to add that in on the vscode side I'll update my answer. – Chris Schmitz Nov 27 '20 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.