I am new to PlatformIO, so please be gentle with me.
I have just ported a script from the Arduino IDE to the PlatformIO IDE and almost everything went straight forward. So far I am very impressed with PlatformIO but I do have one question.
Because my script was so long (20,000 lines) I wanted to split it into smaller chunks so that it would be easier to maintain. I have done this with great success using #include files and storing them in the include folder. However, what I would really like to do, to tidy things up, even more, is to put some include files in sub-folders inside the include folder.
When I do this everything compiles okay, but the file in the sub-folder has dozens of errors about global variables that it can’t find.
I was wondering if I had to specify something in the PlatformIO.ini file to get it to recognise the global variables that are declared in main.cpp?
So, just to clarify, my structure looks like this:
…and the links go like this:
Global variables declared in main.cpp
main.cpp contains: #include “myfile.h”
myfile.h contains: #include “test/test.h”
As I said, everything compiles okay. I just want to eliminate all the error messages in test.h
Thanks for any help.
If you link them like this then incidentally that’s the only way this works. By themselves, your header files are probably not valid (=“being able to be included from everywhere without error”) because they expect that certain files have been included before them in which e.g. some global variables are.
All in all, if you want to split code, you shouldn’t put the C++ implementation code in a header file and then
#include it, but properly split it as a
.h header file with only function and variable prototypes / declarations and a
.cpp file with the implementation.
This is already covered in topics like Splitting cpp files and Tutorial for creating multi cpp file arduino project. It’s a pure C/C++ development topic and has nothing to do with PlatformIO itself really, but we’re still here to give support of course.
I’m sure if you read through the above links and tutorials on how to split C++ code in header and implementations, plus how to declare and define global variables using the
extern keyword and friends, you’ll be able to rewrite your project properly.
If you need concrete help, you’d need to post the complete project, in e.g. Github or google drive.
You are absolutely correct, I do need to learn how to organize my project correctly. I have read through the “Tutorial for creating multi cpp file arduino project” and that is something that I will need to look at further.
In the meantime, I was tinkering around and came across the exact answer to my original question. So, although this may not be the “PC” way of doing things, here is the solution in case anyone else has the same problem or is just curious:
First, I noticed that when I clicked on one of my variables which had a red squiggly line under it, a yellow lightbulb appeared at the side. Clicking on the lightbulb gave me two choices, one of which was: Edit “includePath” setting
When I selected: Edit “includePath” setting, I was asked to: Select a workspace folder…, so I chose my workspace folder from the list it gave me.
This opened an editor titled: C/C++ Configurations
In this editor, there is a list of “include paths” where you can add new paths (which I did).
Also, under Advanced settings, I added the same new path to the “Browse paths” list.
After making these changes all my errors disappeared and everything is now working as expected.
There is just one thing:
After closing the C/C++ Configurations Editor I couldn’t find any way to get back to it.
If you need to make changes to your include paths but can’t find this editor it can be done directly in the json file by going to the following file in the PlatformIO Explorer:
At the top of this file is a warning telling you not to edit the file manually, but if you are careful and only add the paths you need in the correct place and in the correct format you should be okay.