Translating an Arduino project to PlatformIO and C++

I’m trying to transfer an Arduino Teensy3.6 project to platformIO – it was easy and the project compiles but at the moment the project consists of multiple .pde files which I have used to make the large amount of source code easier to navigate, but I want to convert it all to ‘proper’ .cpp and .h file structure.

The problem I have is in understanding how to make all the functions in each .cpp file visible to all the other functions – Arduino treats each .pde file as a single file so it all compiles OK – but if I just rename the .pde files as .h files and #include them in the main sketch the project is reduced to a mess of compiler errors as it either can’t find functions, or complains of multiple definitions.

Is there a proper way to structure my project in a way that doesn’t involve restructuring all my code but which preserves the way I have split up functions across multiple files, or should I just stick with multiple .pde files …?

Let me help – having been through this. Shedding the .pde comfort blanket can be a bit rough at first.

Since it sounds like you had the project working with the Arduino build environment, I would first recommend that you do your .pde -> .h/.cpp translations in that context as a first step.

Step 1: Take all your supporting .pde files and properly create .h and .cpp one file at a time. By methodically going through it this way, you limit the onslaught of compiler errors and you’ll be able to figure out which file needs to include which .h.

By way of example, I would typically work with a very basic main PDE file (in order to leverage the Arduino IDE) that would include my own .h/.cpp library set. I would keep this library set within my Arduino/libraries so the IDE could find it. I could then use a different and more capable editor and do all of the heavy-lifting in the .h/.cpp library files.

Step 2: At this point (still in the Arduino IDE) you should have a single .pde file that includes various .h files. All compiles well…

Step 3: Now its time to move to PlatformIO. Follow the PlatforIO steps for converting a .pde over to a .cpp file. For me, this was as simple as putting in a bunch of forward decorations in the .cpp version:

void startupSequence();
void serialPrintStartupInfo();
void setupArduinoINT0andINT1();

Hope this helps

1 Like