Newbie confused with libraries

Hi @platt-io,

I find the easiest way to get hold of libraries is to go to PlatformIO Registry and fill in the form there. So if I was looking for a library such as “fred” for an ESP-IDF, I would fill in the search term:

"platform:Espressif IoT Development Framework"   fred

alternatively, as I don;t like typing (that will surprise some people who know me!), this:

"platform:ESP-IDF"   fred

Either way, a quick check just now without a library name, just the two options above for the platform, returned 8,656 as the number of libraries which are flagged as being compatible with the development platform you want to use.

Apparently, there is no library called “fred”! :grin:

So, once I’ve found my library and clicked its name to open its page, I see three tabs: “Exaples” - example sketches using the library, “Installation” - a list of different methods of installing the library, “Headers” - a list of the headers provided in the library, to be #included in your code as required.

I usually just add the lib_deps = vendor_name/library_name@^n.n.n option in the platformio.ini file for whichever project needs to use the library.

Looking around GitHub can lead you down all sorts of incorrect rabbit holes, causing much wailing and gnashing of teeth etc. Use the library search feature as described above and it should just work.

Not all libraries you find on GitHub are usable with PlatformIO (or even, with the Arduino IDE). You also need to consider the framework in use, you can have a library that will work with a project in PlatformIO, but only if you are using the Arduino framework as the library is written to use various bits (technical term) from the Arduino Language, or #defines created in the various Arduino header files. (I’m looking at you Helios! :wink:)

Having said that, some libraries (on GitHub and elsewhere, even in the PlatformIO library system, do strange things and cause the occasional header file not to be found. I believe Adafruit’s Software Serial library has a problem which causes the Library Dependency Finder a small problem in locating the header file that it needs. Other libraries may be similarly affected.

Assuming that the library on GitHub is written in a “proper” manner, which PlatformO understands, then using the GitHub URL in lib_deps is perfectly acceptable and should result in the library being downloaded and used. Of course, libraries in GitHub can change frequently and I’m not a big fan, even with my own library (libraries) on GitHub!

The library author is probably unaware of PlatformIO. Hard to believe I know, but that’s life. You are better off looking elsewhere, or, if you feel the urge and the licence allows, clone the repo or download a zipfile and modify the library to work with PlatformIO.

You can unzip the files anywhere you like. As long as you end up with a structure where the source and header files are readable by the compiler. When I write my own libraries I use a couple of methods:

  • If I’m creating a class (C++) to use in a single project, I will just drop the files into the src directory, or, I might occasionally, drop my class headers and .cpp files into lib in a folder reflecting the class name.

  • If I’m writing a library to use in Arduino, or PlatformIO, I’ll create a separate library directory (Mine is called PlatformIO_Libraries at the top level, and under than have a separate directory for each library, viz:

    PlatformIO_Libraries
        AVRusart
             AVRusart.h
             AVRusart.cpp
       CBuffer
             CBuffer.h
             CBuffer.cpp
       ...
    

    In my platformio.ini file, I point lib_extra_dirs = /home/norman/SourceCode/PlatformIO_Libraries/ and whatever library I have #included the header for, is found and compiled into a static library (libAVRusart.a for example) and linked with my code to create the final executable. With this option, I have no need for lib_deps for my own stuff, but if I’m using some other libraries, I would use that too. Obviously?

Another alternative which I don’t use often, is to have lib_deps point at the full path to the folder where the source and header files live:

lib_deps = 
    file:///home/norman/sourceCode/PlatformIO_Libraries/AVRusart
    ...

(Yes, if you have lib_deps spread over many lines, you must indent at least 3 spaces. ask me how I know!)

You can indeed. Add build_flags = --verbose and the full command lines passed to the various stages of compilation will be revealed in horrific detail!

HTH

Cheers,
Norm.