How to add a class to multi project worspace?

I am using PlatdormIO with VScode.
The workspace include several ESP32 projects which communicate with each other.
Since these projects have several several classes in common, I created an .h file at the root of the workspace and tried to include it like regular lib, or with include <…/xx.h>, which failed compilation.
I also tried to reference the class directly without the #include - failed again.

How can I create a class that will be used by several projects within same workspace/

Create a folder in which you create a new library folder that then has your .h and .cpp files, and reference it via lib_extra_dirs.

Sorry, I am not sure I follow. the drive is to to share a .h file among several projects within same workspace.
Are you suggesting to create a lib?

I was sure it’s just an include or referencing from the source cpp file.

Yes, if you want to do it the proper way as PlatformIO intents it. Shared code (whether it be only a header file or with accompanying code or not) can be put in a library that the projects refer to in their platformio.ini by declaring it as a lib_extra_dirs.

If you don’t care about that, you can hack it in using build_flags in each project’s platformio.ini:

build_flags =
   -I<path to folder where your .h file is>

PlatformIO doesn’t know about “multi workspaces”, that’s a VSCode concept. PlatformIO projects are standalone from each other – unless you edit their configuration to refer to a common libary folder, for example.

1 Like

Thank you for explaining it. Now it’s clear.