Multiple, unique, programs in a Project

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

1 Like

I don’t quite get this, can you show an example? Is “several programs under a single project directory” equal to “multiple .INO sketches located in the same folder”?

Thanks for your response,

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.

1 Like
  • 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?

Thank you very much. I’m anxious to try what you suggest.

1 Like

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.

1 Like

@piGuy, how did you get on? I’m in a similar position to yourself. My project uses two Arduino Unos, so I’m not sure how I can separate the environments.

I had this all working fine yesterday, but today on start up, PlatformIO extension would not activate because the platformio.ini was not in the root folder.

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.


Thanks, I tried that before you replied and got it working again. Once you know, you know!


platformio.ini can manage multiple programs with environments, notice [env:main] specifies build_src_filter = +<main.cpp> also notice that .pio/build has a build folder for main and test_thru