This is NOTt about multiple .cpp and .h files under a single src directory - all needed to compile one program.
I’m very new to PlatformIO (less than a week), trying to migrate from Arduino IDE. One thing I found easy in Arduino but can’t seem to do in Platform IO is to have several programs under a single project directory. The idea is that they could share libraries I have added to the project directory and other common things like platformio,ini. But, they could be separately compiled and uploaded…
As a test, I created a project and created programA.cpp, which I compiled and ujploaded to my device. I then added a folder under the project and added a src folder under that. I then created programB.cpp, under that new src folder and tried to compile and upload that new program. Problem is it compiled and uploaded programA.cpp rather than programB.cpp.
Is there a way I can accomplish what I am trying to do. I sure would find it inconvenient to start a new project for each of these related programs. Thank you
No. Each .ino file would be in its separate folder, but all under the same overarching folder.
These could be different versions of the same sketch, or related sketchs, often stuff I just want to try out but related to the same project. I know Arduino IDE does not have a project concept, per se, For me the folder just under the Arduino folder is like the project designation. All my .ino files that are related to each other are in folders under that folder.
It then is easy, and fast, to go from one of these sketches to another.
I really like the idea that in PlatformIO I can put project related libraries in the project folder. Easy to disseminate your project to others. Would be nice if all of my related PlatformIO programs could share libraries without having to have a separate copy go along with each of program, or by sticking the libraries in the global library space.
In VSCode you can have multiple workspaces open at once. See the documentation. That of course requires you to duplicate your project once you work on a new variant of the project. You can copy-paste the folder and then execute the “Rebuild Intellisense” so you don’t have to go through the project creation wizard. Also, libraries can more easily be declared via the lib_deps declaration in the platformio.ini, so to keep project’s in sync, you just need to keep the platformio.ini in sync.
In the same environment, you can create multiple environments.See the documentation. By additionally using the src_filter, you can control which files are compiled in which environment by excluding specific files. This would allow you to have one “real” project with one shared platformio.ini and lib folder , with the src folder containing multiple cpp/h files which you manually select in each environment
there is the src_folder option, by which you can chooce the folder names where the source files are located. You can write a simple python script which, based on the used environment or some other variable, rewrites the src_folder or PLATFORMIO_SRC_DIR variable before build. This is just a fancier version of the thing above but now the files don’t have to be in the same src/ folder.
Ok, so we’re talking about something like the Arduino sketchbook folder, where you would have subfolders for each program, and then the .ino file(s) in each of the subfolders, but with one common folder for libraries?
Maybe the platformio.ini lib-extra-dirs parameter?
Yes, that is exactly what I mean. To me a 'project" would include all versions, and all related programs. But, that is just my idea and comes from the way I work. I’ll definitely look into parameter you suggest. Thanks for your help.
Open a new window (File -> New Window) , and add the Sensor Hub and Gateway folders separately the workspace (File -> Add Folder to Workspace for each). This will now be a multi-root workspace, and as far as PlatformIO is concerned, two separate project folders. Save the workspace (File -> Save Workspace As) if you want to be able to save the configuration to a file so you can re-open it at a later date. Workspace can also store other configuration into such as installed plugins, editor settings, etc.