How to share common code between projects?

I have been googling this for a while, but can’t find any answers that seem to fit my situation. However I found something that looked promising regarding setting up a lib_extra_dirs option in platformio.ini. But this option is depricated and I don’t know what to do as an alternative.

I have a bunch of projects (each is a sensor in a home automation system) that are quite similar and each use a few common functions that I have written. Currently each project has its own copy of these common fuctions. Not a good idea!

How do I setup a library of these functions so that all projects can access them? My folder structure is as follows…

main folder
     project A
          .pio
          .vscode
          include
          lib
          src
          test
          .gitignore
          platformio.ini
     project B
     project C

I’m happy to change the folder structure if it is not ideal. In any case, where should I put the common source code and how will the compiler find it?

Cheers

You can still have lib_deps (docs) in each project that point to common library folders via a symlink (so that no copy is done), e.g.,

lib_deps =
   symlink://../common_libs/my_common_lib_a
   symlink://../common_libs/my_common_lib_b

That would have previously been lib_extra_dirs = ../common_libs.

1 Like

Perfect, thanks Max.

I can’t get this to work! Adding such a symlink line for my library in the lib_deps section results in a MissingPackageManifestError. Do I need to also create a ‘library.json’ file in the library folders as well?! (tried that too: didn’t work.) This all seems way too complicated for such a simple and common task. The deprecated lib_extra_dirs does still work, so I’m good until that gets pulled. Why a simple and effective solution is being killed is interesting. BTW, simply removing the symlink line doesn’t get rid of the error it caused. You have to hunt down and delete the .pio-link file it created to get back to sanity. (That experience alone cost me a couple of hours!!)

Got quite all of a sudden.

I am having the same issue. I was able to create a json file but that’s a lot of trouble, espectially if you don’t know much about manifest or platformio inner workings.

This is what worked for me. I hope it helps …

  1. I created a new folder for my library
  2. I created a src folder (contains the *.cpp and *.h library files) under the new library folder
  3. I created a json file and put in the library folder
  4. I added this line to my lib_deps section of platformio.ini

symlink://C:\Users.…\HALibrary

where HALibrary is the folder created in step 1.

That’s it. All works fine.

The following DOES NOT work for me:

lib_deps =
symlink://··/··/piolib\FLog
symlink://··/··/piolib\ScaledKnob

The following DOES work for me:

lib_extra_dirs =
··\··\piolib

Any symlink attempts fail with the error…

MissingPackageManifestError: Could not find one of ‘library.json, library.properties, module.json’ manifest files in the package

I tried with path slashes in both directions. I also tried absolute paths.

(The ‘piolib’ folder exists one level below my project folder.)

I’m clutching at straws here, but the only thing I can see that is different is that I have my library folder at the same level as my project folders.

Crazy if that makes a difference, but possibly worth a try???

I’d like to ping this thread as the proposed solution results in an exception in PlatformIO 6.1.15:

MissingPackageManifestError: Could not find one of ‘library.json, library.properties, module.json’ manifest files in the package

I’ve got roughly 60 libraries in my custom library folder that i can no longer use with the deprication of Lib_extra_dirs. And love to have a solution for this issue. Going to all libraries and adding boiler plate manifests definitively is not a good solution in my opinion.

Well, I think it’s also a matter of taste.

Personally, I think it’s good that these global library folders are finally being abolished. This has often led to problems in the past. (At least that’s how I felt).

I prefer to decide for myself which of the libraries lying around in a “libraries collection folder” should be used for the project. And that works very well with “symlink”.

A proper structure in a library should be a prerequisite anyway (./src, ./examples, ./docs etc.). I don’t think it’s so bad to add a minimal “./library.json”.

What I have noticed, however, and here an improvement would be possible:
Even a nearly empty “library.json” is accepted without problems, as long as it contains “{}”.

If this behavior is also adopted (by the developers of PlatformIO) for a non-existent “libary.json”, your problems should be solved.

As mentioned above, the symlink command worked for me where I used an absolute file path to my library. Now however I would like to make the symlink file path relative, so I can develop on two different PCs.
My file structure is

 Home Automation/
|- HALibrary/
|   |- src/
|   |   |- commonutes.cpp
|   |   |- commonutes.h
|   |- library.json
|- Gateway/
|   |- src/
|   |   |- main.cpp
|   |- platformio.ini
|- Sensor/
|   |- src/
|   |   |- main.cpp
|   |- platformio.ini

I changed platformio.ini to

symlink=…/HALibrary

but got this error

Resolving ATmega328PB dependencies…
Library Manager: Installing …/HALibrary
UnknownPackageError: Could not find the package with ‘…/HALibrary’ requirements for your system ‘windows_amd64’

So I tried using lib_dirs instead as per

lib_dirs =
…/HALibrary

and that works except for a warning

Library Manager: Installing lib_dirs
Warning! Could not find the package with ‘lib_dirs’ requirements for your system ‘windows_amd64’

I can live with a warning, but I’d prefer to have a ‘clean’ solution if possible. Anyone got a solution for a relative symlink file path or a warning free lib_dirs?

File and folder structure (should match yours)

.
├── HALibrary
│   ├── src
│   │   ├── HALibrary.cpp
│   │   └── HALibrary.h
│   └── library.json
├── Gateway
│   ├── include
│   ├── lib
│   ├── src
│   │   └── main.cpp
│   ├── test
│   └── platformio.ini
└── Sensor
    ├── include
    ├── lib
    ├── src
    │   └── main.cpp
    ├── test
    ├── .gitignore
    └── platformio.ini

Then the “HALibrary” can be added by symlink:// as follows:

lib_deps =
  symlink://../HALibrary

I used a very basic library.json for the HALibrary:

{
    "name" : "HALibrary",
    "version" : "1.0.0"
}

Output:

LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 6 compatible libraries
Scanning dependencies...
Dependency Graph
|-- HALibrary @ 1.0.0
Building in release mode
Compiling .pio\build\uno\src\main.cpp.o
Compiling .pio\build\uno\lib58a\HALibrary\HALibrary.cpp.o

Well done sivar2311, that works perfectly.

Thank you.