How do I share code between two projects?

I have a folder containing multiple .h and .cpp files. How do I include this in two different projects? Ideally I would just put the link to this folder somewhere in the platformio.ini, but it seems the only way I can get .cpp files to work is if they are in the src folder. I’ve tried a lot of different things.


You can put library folders which you want to share in a folder (say C:\Users\<user>\Documents\my_libraries) and then use lib_extra_dirs with the absolute or relative path to my_libraries .

Thanks Max.

I have tried that but then how do I include the files?

lib_extra_dirs = C:\Users\user\Documents\arduino\libraries
And then my library is in C:\Users\user\Documents\arduino\libraries\mylibrary
the library.json has a “name” of “mylibrary” and one of the “headers” is “Utility.h”.

I have tried:

#include <mylibrary/Utility.h>
#include <Utility.h>
#include “mylibrary/Utility.h”
#include “Utility.h”

None of them work it seems. The message always pops up “No such file or directory”

Thank you.

This should be correct. Have you made sure to press the build button at least once? Does it show mylibrary in the dependency graph?

You don’t need one for simple libraries, just the folder and the code files.

I’ve figured it out. My library had some of its files in a subdirectory called “src” so it was ignoring everything else in the root.

Thanks very much for the guidance. :+1: