Confusion with designating library folders

I’m struggling to include files from a library using lib_deps. I have a library repo that looks something like this:

|--lib
|  |
|  |--Bar
|  |  |--src
|  |     |- Bar.cpp
|  |     |- Bar.h
|  |
|  |--Foo
|  |  |--src
|  |     |- Foo.cpp
|  |     |- Foo.h
|
|- platformio.ini
|
|--src
   |- main.cpp
   |- another_file.cpp
|
|--test
   |- some_tests.cpp

Then, in another project, I want to use code from Foo and Bar, but not the files in src, so I include them with:

lib_deps = 
    https://github.com/Me/MyLibrary

As is, it does not compile, because the compiler is looking in src and not lib – if I move the four library files (Foo.h, etc.) into src, the main project will compile. But I don’t really want to do that (I want to have things organized as above for testing), so I’ve tried using a library.json file in the root of the library repo:

{
  "name": "FooBar",
  "version": "0.0.0",
  "build": 
  {
    "srcDir": ".",
    "srcFilter": "+<*> -<src> -<test> +<lib>"
  }
}

This still does not compile, and the compiler says it can’t find the header file, Foo.h.

I expected srcFilter to tell the compiler where to look for files and, according to the documentation, setting srcDir to . should tell it to recursively compile all directories (as filtered in the next line).

So, what am I missing? How can I include files in the build using the library structure above?

This is a project folder structure, not a library structure!

Please see Creating Library — PlatformIO latest documentation

Thanks for the pointer – that puts me back on track. I had gotten off track because I was also trying to build tests and went down the proverbial rabbit hole.

Let me explain.

The reason there was a main.cpp was so that the library could be tested in a limited environment, not the glorious project that it’s being included in. My plan was: do some native tests, then some platform-specific tests of generic code, then live, limited tests with hardware inputs (hence main.cpp). The first two are straightforward, but you need a setup() and loop() for the last.

In retrospect, better would be to use the recommended library structure and make my limited, hardware test an example.

Is there an easy way to run an example when you’ve opened the root folder? In the past, I’ve just opened an example folder and put lib_extra_dirs = ../.. in the .ini file. It works, but it seems a little clumsy when doing development work.

See my example library in this post: Platformio.ini for local building - #2 by sivar2311

This probably meet your requirements.

It is a library which is able to build the examples when you clone the repository.

Perfect. I was about to start trying that, but an example gets me 99% of the way there.

The symlink is nice. Saves having to remember how many .. to append to lib_extra_dirs. :grin:

1 Like