Lib folder VS lib_extra_dirs , need help to understand

Hello everyone

I’m new user of PIO.
I try to move from Arduino IDE to Visual Studio Code with PlatformIO.

I try to add library by using to not modify my actual structure with arduino :

lib_extra_dirs = ~/Documents/Arduino/libraries/xxx

It works well for some library
but sometimes it doesn’t, it compile but when it’s linking firmware.elf isomething get wrong and I got a lot of “undefine reference to”

If I directly had the library to lib folder of the project, there is no problems

I don’t understand what is the difference in my different libraries which product this different beavior :frowning:

can someon help me to understand

thanks in advance

Congratulations! A step in the right direction :slight_smile:

Don’t do this! This will lead ALWAYS to problems.
This is one point where there are differences between ArduinoIDE and PlatformIO.

On PlatformIO (and the rest of the world) you’ll have libraries installed per project.

This may seem unusual at first, but it makes much more sense.

You can use the same library for different projects but in different versions.
This is the only way to ensure compatibility between your project and the library.

Imagine you have different projects using ArduinoJson 5.0.
Now you want to make a project which depends on ArduinoJson 7.0.
Using a global library directory all projects which depends on ArduinoJson 5.0 will break.

However, you will soon get used to it.

Only install the libraries that you need for the current project.

Libraries are installed usally via the platformio.ini file

An example for ArduinoJson 7.0:

lib_deps = 
  bblanchon/ArduinoJson @ ^7.0.0

@sivar2311 thanks a lot for your quick reply

I totally understand your point of view
I will try to do as you suggest

How do you manage your own library which are not on GitHub ?
I mean I built some libraries which I use on arduino, I like when I correct some bugs that they are corrected for all my projects
I agree, if I do big changes, it can break some projects

So 3 questions from a noob :

  • what do you use to manage your own not online libraries ?
  • how do you increment their version ?
  • how do you like PIO to your own not online libraries ?

Thanks a lot again :slight_smile:

I am not on my pc right now. Sorry for the short answer in advance.

  1. See documentation lib_extra_dirs — PlatformIO latest documentation
  2. Provide your libraries with a library.json file (see library.json — PlatformIO latest documentation)
  3. ?

Sorry my questions were not clear

  • what do you use to manage your own library on your computer ? (git on your computer ? An other software ?)

  • with this software, can you increment the version ? ( I guess it’s the goal of this kind of software)

  • in this case, how do you link your personal library in PIO for your projects ? (Do you have to register the library somewhere in PIO? Do you have to add “lib_deps” with a direct link to your folder ? …)

Thanks again for your help
I will try to start to have good habits since the begging :sweat_smile:

I think everything I’m asking is here :
Library on PIO

I’m going to try it :slight_smile:

This depends on the type of project and the library.

Usually this is a multi-step development:

  1. If it is just about splitting the code I write a simple header file (.h) and the implementation (.cpp) directly in the src directory. (That’s how it usually starts for me)
  2. If I realize that it will be more complex I create the necessary files and structures in the lib directory
  3. If I realize that I can also use this in other projects, I add a library.json and upload the library to GitHub, remove the files from lib folder and then include the library with lib_deps = https://github.com/sivar2311/<repository>)
  4. If I think the library is really good and also useful for other users, I add examples, write a readme and publish the library in the PlatformIO registry

If it is just a personal library that I have on GitHub, a
lib_deps = https://github.com/<username>/<repository>
is sufficient.
Example:
lib_deps = https://github.com/sivar2311/FatUSBMSC

If it is a library that I have published on the registry I use
lib_deps = <username>/<LibraryName>
Example
lib_deps = sivar2311/FatUSBMSC

Very clear reply, thanks :grin:

I tried it and it works well online.

What about offline computer ?
Do you know how I can manage library without publishing online ?

This is how I would handle such a scenario:

Folder structure

+-- some_root_folder
    |
    +-- libraries
    |   |
    |   +-- library1
    |   |   +- examples
    |   |   +- src
    |   |   |  +- library1.h
    |   |   |  +- library1.cpp
    |   |   +- library.json
    |   |
    |   +-- library2
    |   |   +...
    |   |
    |   +-- library3
    |   |   +...
    |   
    |-- projects
        |
        +-- project1
        |   +- include
        |   +- lib
        |   +- src
        |   |  +- main.cpp
        |   +- platformio.ini
        |
        +-- project2
        |   +...
        |
        +-- project3
        |   +...

Then in the projects platformio.ini:

lib_deps =
  library1 = file://../../libraries/library1
  library2 = symlink://../../libraries/library2

Either symlink:// or file://, whichever fits better - see documentation

Thank you, very clear, I will try it

I guess you can not handle the libraries version
But it’s ok for me :slight_smile:

The version control outside of PlatformIO and up to you.

A very basic solution could be multiple version directories per library:

+-- some_root_folder
    |
    +-- libraries
    |   |
    |   +-- library1
    |   |     |
    |   |     +- v100
    |   |     |   +- examples
    |   |     |   +- src
    |   |     |
    |   |     +- v101 
    |   |     +- v201 
    |   |
    |   +-- library2
    |   |    +...
    |   | 
    |   +-- library3
    |        +...

Of course the lib_deps must then respect the subdirectory.

1 Like

@sivar2311 Thanks a lot for your explanations and your time, it really help me