PlatformIO imported arduino project compiles until saved

I’ve recently switched from Arduino to the PlatformOI IDE for the ease of use tools. I’m using VScode and the latest PlatformIO to program a Teensy 3.6. I have a rather ‘lengthy’ program(for me at least…2432 lines at the moment) and am using multiple libraries including the Teensy audio library.

When I import the project into the platformIO it compiles the .ino just fine and I can make changes and put them unto the teensy and all is well. But when I save the project and come back to it it wants to compile as a.cpp… and I get a bunch of “error: redefinition of…” errors.

I’m assuming this is related to the frequently asked question … Convert Arduino file to C++ manually
but all I can really tell from that is that void functions need to be declared before they are used in the sketch.

I’m just curios if there is anyway to get it to work like it has just been imported. I mean I could paste all the text into a new arduino sketch and re-import the arduino sketch every time I want to save and come back to the project.

Any suggestions or help is appreciated.

I can give you my basic understanding of it, arduino IDE is basically a c compiler under the hood, what PlatformIO calls main.cpp is roughly what arduino calls yourprojectname.ino
Now one thing that arduino does do, is look for functions before it starts compiling so it knows where they all are even if you have them at the end of your code, you have two options for this, you can either move all your functions cut and paste style from where they are now to the top of the code , i.e. after the #includes and #defines but before the void setup like this

#include <somelibrary.h>
#define SOMESTATICTHING 2

// Move ALL your functions HERE

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Or you can add the definitions (copies of the function first line with a ; at the end) of the functions in the same place and leave the functions where they are. Like below, remember to add a ;(semicolon) to the end of your function headers. This way allows you to have functions call other functions even if they are out of order as would be the case if you did it the first way.

You can then save the file as whatever is was called before .cpp so just changing the .ino to .cpp

#include <somelibrary.h>
#define SOMESTATICTHING 2

// PUT Function Headers HERE

void mytestfunction(int VAR1);
int myfunctionthatreturnssomething(int Something);

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

void mytestfunction(int VAR1) {
    //Some Stuff
}

int myfunctionthatreturnssomething(int Something) {
    //Do some more stuff
    return Something;
}
1 Like

Thanks for that. It’s just weird to me that it works as is after being imported(“Import Arduino Project”) but when I come back to the project after saving and exiting then it expects a different formatting for cpp.

And you haven’t changed the extension of the file at all? i.e. because of this prompt?

image

As if you have… it’s becase you’ve changed the file extension, hence the rules :wink: I just ran through an import myself, and it didn’t rename the file on it’s own, and still worked after restarting VSCode and re-opening the project.

It seems that if you leave the file with .ino formatting. PlatformIO will be more tolerant of at least some of the Arduino rule breaking. As soon as you rename the file to have a .cpp extension though, you’re expected follow the C++ rules properly though. Meaning you basically need to:

  1. Add #include <Arduino.h> to the top of your main source file
  2. Declare all functions before use - ie. as Foxa said, either put them before your setup and loop, or copy the first line.

Thanks Pfeerick,
I do get that pop up when I load the file. Didn’t realize progress was remembered when exiting VScode. I was doing a save as… ‘nameOfProject.ino’ which is under the C++ catagory and then when I would go to load the project and recompile I think it tries to compile as a ‘nameOfProject.ino.cpp’. I’ll keep learning/messing around with it. Thanks All

Doing the ‘save as’ would be no different to renaming the file… as that’s all that is happening… you’re saving the text file with a different file extension.

If you get into the habit of doing the above steps, regardless of if you use the Arduino IDE, PlatformIO or some other C++ compiler like Atmel Studio, etc, the code will should work without changes, so it’s a good habit to get into. Keep at it… it’ll get easier! :smiley: