Development libraries -- how to include in a project?

I’m coming from the Arduino IDE, so bear with me…

Let’s say I have a project that relies on a packaged library – let’s say an IMU library from one of the usual suspects (e.g., SF, Pololu, etc.). I want to modify the library to add functionality to it, and I want that to be available to other projects, as well.

So, I first fork the library on github. Then I include it in my lib_deps:

lib_deps = https://github.com/.../myForkedIMULib

platformio now makes a local copy in my project tree, yes? I can edit the code, but it doesn’t appear to be under git, so changes that I make are not pushed back to my repo. Worse, I make another project, and it makes its own copy, so now things are getting out of control.

I did have a couple of libraries in the global lib, but now they won’t update. I get the error discussed here: VCS: Could not process command ['git', 'pull', '--recurse-submodules'] · Issue #94 · platformio/platformio-home · GitHub, which seems to imply that if I make changes to a cloned repo, it breaks things (which makes no sense to me – I want to be able to update the libraries!).

So, what should I do? Where should I put libraries that are under development so that

a) I can use the changes in multiple projects (preferably wo/ having to update everything from project to project), and
b) I can use git to manage them

Happy to answer questions. Hard to explain when I don’t know much of the basics.

There are mainly three options:

  • Put it in the global ~/.platformio/lib directory. Of course, it won’t update automatically anymore.
  • Put them in a directory outside ~/.platformio and outside your project folder and refer to the directory (or rather the parent directory) using lib_extra_dirs. Again, no automatic update.
  • Put them into the lib directory within your project folder. This approach is useful if the modification is for a single project only. No automatic update either.

Don’t make changes to the code in .pio/libdeps/.... .pio is just a temporary directory. I often delete it to force a clean build.

Automatic updates for modified libraries are not possible. It is not a restriction of PlatformIO but rather an inherent restriction of two source code copies that are independently modified. What should happen if the original maintainer changes the same line of code as you did? Should your change be overwritten? Or should it have priority over the other change? Or should both lines of code be used? In what order? Only you can tell.

In order to keep up with the latest changes of the library, use git tools. In many cases, you will be able to pull the latest changes and merge them automatically. In case of conflicts, you will have to merge them manually.

Thanks for the summary. That helps.

I didn’t think of this – thanks for the warning.

Hello Manuel,

For my development, I chose the second option of the three that you provided.
During the development of the application project, I made a local library for it with the intent
to use it later on for other application projects. The first application project now compiles and runs.
For the development of the second application (using a different board, by the way),
I created a lib directory in the directory that contains the two application projects,
and moved the library there. In the first application project, I updated lib_extra_dirs accordingly,
and my first application now compiles again. So far so good!

Now in order to make the library work also for the second application project,
I would like to open the files in the library and edit them, without compiling of course.
Compiling (and linking) will be done in the two application projects (for different boards).

Now I can open them by using “open file” in the top left corner,
and then I need to browse through my directory structure to find it.
That works, but it is somewhat cumbersome. Is there perhaps any way to create
something that I would call a library project, so that it can be added to my workspace?
That would allow the files to be opened just like the application project files.

I did put in some effort to find information on this topic, but unsuccessfully so.
The closest I could find is

but this talks only about the file structure, and assumes the library is external.
My question is about a home-brew library in more or less the same file structure.

Many thanks in advance!

Ah, as luck would have it, I just found a “add folder to workspace” entry in the file menu.
That works precisely as expected. Problem solved!