Not able to use private library from project nor from test

I am trying to make a simple demo setup for myself to understand how libraries are working in platform io. I am getting nowhere… According to what I read is should be as simple as:

  • add some folder to my [project]\lib, e.g. SomeLibrary
  • add a header to the library structure
  • optionally add the library as lib_dep in platformio (but since it’s private, I shouldn’t have to)
  • use the library in project and / or test code.

I have the following platformio.ini

[env:native]
platform = native
test_transport = custom
lib_deps = SomeLibrary

the library is located in:
[project]\SomeLibrary\include\SomeClass.h

with the following content

class SomeClass
{
    public:
    SomeClass()
    {
    }
};

in [project]\src I have a almost empty main.cpp:

#include <SomeClass.h> // <== file can not be found

int main()
{
}

Ultimately resulting in this error:
src\main.cpp:1:23: fatal error: SomeClass.h: No such file or directory

What I am doing wrong?

PS: tried many variations in the library. without the “SomeLibrary” parent folder. moved the SomeClass.h to the lib\SomeLibrary\src folder. Used .hpp as extension. All results in the same error.

This is where your problem lies:

I tried exactly that and received this error, as indeed you appear to have:

src/main.cpp:1:10: fatal error: SomeClass.h: No such file or directory

I then shifted SomeClass.h from lib/SomeClass/include to lib/SomeClass. This time the compilation went like:

Processing native (platform: native)
--------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 1 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <SomeLibrary>
Building in release mode
Compiling .pio/build/native/src/main.o
Archiving .pio/build/native/lib78a/libSomeLibrary.a
Indexing .pio/build/native/lib78a/libSomeLibrary.a
Linking .pio/build/native/program
========================= [SUCCESS] Took 0.59 seconds

So, don’t have the library header files in a separate include directory. Looking at the compiler options when compiling the library, I see this:

...
Building in release mode
g++ -o .pio/build/native/src/main.o -c -DPLATFORMIO=50101 -Iinclude -Isrc -Ilib/SomeLibrary src/main.cpp
...

You will note that the -Ixxx options, where the compiler should search for include files, only includes include, src and lib/SomeLibrary but not lib/SomeLibrary/include.

Oh yes, when #including your own library headers, it’s best to avoid using the ‘<’ and ‘>’ angle brackets as they are for system headers, you should be using double quotes.

HTH

Cheers,
Norm.

1 Like

Hi Norm,

Thanks, I found that myself just a minute before you answered. A bit weird, because the platformio page strongly suggests to structure libraries in lib[libraryname]\src and lib[libraryname]\include.

Thanks for the tip about double quotes too. Will follow that guideline too.

But, when all seems to be fine now, intellisense still isn’t happy as it claims it can’t find the header (I also can’t go to its definition). Do you recognize that problem too?

Again, it compiles fine now, just really annoying to code without intellisense.

image

Do an IntelliSense rebuild with Ctrl+Shift+P → Rebuild IntelliSense.

1 Like

Oh my… didn’t even know that existed! Thanks a bunch!!

Hi @basprins,

Glad we got you sorted.

Intellisense is part of VSCode and not anything to do with PlatformIO itself. It does get a little confused if you make changes to a file, sometimes, and starts “squiggling” left right and centre. Usually a quick build of the project resolves the issue, or as @maxgerhardt has mentioned, rebuilding intellisense does the job.

Intellisense is coming along nicely these days, it has “had its moments” in the past. Personally, I don’t tend to pay much attention to it.

Hmmm. Did you refer to this document> Creating Library — PlatformIO latest documentation

If so, that’s for building libraries that can be uploaded to the PlatformIO libraries repository. So if I created “AVRsomething” as a library and published it, anyone could then add “NormanDunbar/AVRSomething” to their lib_deps and get my library.

For libraries written specifically for a project of your own, the docs are a little sparse (or my searching capabilities are not as good as they should be!) but a Google search came up with this post from @maxgerhardt

and the link to the readme mentioned takes me to this location:

Where we find an example of how to create a library. The gist of which is:

This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.

The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").

For example, see a structure of the following two libraries `Foo` and `Bar`:

|--lib
|  |
|  |--Bar
|  |  |--docs
|  |  |--examples
|  |  |--src
|  |     |- Bar.c
|  |     |- Bar.h
|  |  |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
|  |
|  |--Foo
|  |  |- Foo.c
|  |  |- Foo.h
|  |
|  |- README --> THIS FILE
|
|- platformio.ini
|--src
   |- main.c
...

Which does document that under lib you should have a separate directory for each library, and under those, the source and header files for the library are found in src. I have to admit, I’m building a pile of libraries for a new book, and I’ve got the source and headers located in the lib/library-name directory as opposed to lib/library-name/src directory. It works fine though.

HTH

Cheers,
Norm.

1 Like

Hi Norm, I think I missed your last reply completely…! Thx for the lengthy and detailed answer. There is something magical about the attitude of people on platform io forums!

Again very helpful! Thx

1 Like

We aim to please. :wink:

Cheers,
Norm.

1 Like