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???