PlatformIO Community

Dependency of local Library not found

Hi guys,

I put a local library into my project root’s lib folder and the structure looks like this:

MyProject/
   lib/
      MyLib/
         src/
            MyLib.h
            MyLib.cpp
      library.json
   src/
      main.cpp
   platformio.ini

MyLib depends on another library, in this case it’s PubSubClient, so I have included it in both MyLib.h and MyLib.cpp via

#include <PubSubClient.h>

and in library.json I have put

{
  "dependencies": [
    {
      "name": "PubSubClient"
    }
  ]
}

In main.cpp I have included my lib like this:

#include <MyLib.h>

Now, when I try to build the project, the console says

[...]
Compiling .pio\build\nodemcuv2\lib8ad\MyLib\MyLib.cpp.o
lib\MyLib\src\MyLib.cpp:2:26: fatal error: PubSubClient.h: No such file or directory

**********************************************************************
* Looking for PubSubClient.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:PubSubClient.h"
* Web  > https://platformio.org/lib/search?query=header:PubSubClient.h
*
**********************************************************************

 #include <PubSubClient.h>
                          ^
compilation terminated.

I would have expected that PIO automatically figures out that PubSubClient needs to be downloaded and makes it available within the project.
A workaround is to put PubSubClient to lib_deps in platformio.ini, but I think it would be a cleaner approach if my local lib would manage its dependencies on its own and the main project doesn’t even know about the “nested” ones…

Is this possible? Am I doing sth. wrong?
Thanks for your feedback!

1 Like

That’s how I thought it worked too.

What does the dependency graph show when you start your build? Does it show PubSubClient under MyLib?

i.e. this thing …

LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 41 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <ESPAsyncTCP> 1.2.2
|   |-- <ESP8266WiFi> 1.0
|-- <ESP Async WebServer> 1.2.2
|   |-- <ESPAsyncTCP> 1.2.2
|   |   |-- <ESP8266WiFi> 1.0
|   |-- <ESP8266WiFi> 1.0
|   |-- <Hash> 1.0
|-- <FauxmoESP> 3.1.0
|   |-- <ESPAsyncTCP> 1.2.2
|   |   |-- <ESP8266WiFi> 1.0
|   |-- <ESP8266WiFi> 1.0
|-- <ESPAsyncWiFiManager> 0.22
|   |-- <ESP8266WiFi> 1.0
|   |-- <DNSServer> 1.1.1
|   |   |-- <ESP8266WiFi> 1.0
|   |-- <ESP Async WebServer> 1.2.2
|   |   |-- <ESPAsyncTCP> 1.2.2
|   |   |   |-- <ESP8266WiFi> 1.0
|   |   |-- <ESP8266WiFi> 1.0
|   |   |-- <Hash> 1.0
|-- <EasyButton> 1.1.1
|-- <ESP8266mDNS> 1.2
|   |-- <ESP8266WiFi> 1.0
|-- <Ticker> 1.0
|-- <EEPROM> 1.0
|-- <ArduinoOTA> 1.0
|   |-- <ESP8266mDNS> 1.2
|   |   |-- <ESP8266WiFi> 1.0
|   |-- <ESP8266WiFi> 1.0
|-- <ESP8266WiFi> 1.0
Building in release mode

Hi pfeerick,

thanks for your answer.
I took a look at my dependency graph, but it shows only one layer and no nested dependencies (esp-libraries is another dependency from one of my GitHub repositories):

[...]
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 32 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <esp-libraries> #14a83c0
|-- <ESP8266WiFi> 1.0
|-- <MyLib>
Building in release mode
Compiling .pio\build\nodemcuv2\src\main.cpp.o
[...]

Even if I set lib_ldf_mode to deep in my platformio.ini (in the root project), it does not change anything:

[...]
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ deep, Compatibility ~ soft
Found 32 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <esp-libraries> #14a83c0
|-- <ESP8266WiFi> 1.0
|-- <MyLib>
Building in release mode
Compiling .pio\build\nodemcuv2\src\main.cpp.o
[...]

UPDATE
When I run the build step in verbose mode (-v), it says that the PubSubClient library is being ignored. But why is that?

[...]
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 32 compatible libraries
Scanning dependencies...
Warning: Ignored `PubSubClient` dependency for `MyLib` library
Dependency Graph
|-- <ESP8266WiFi> 1.0 (C:\Users\Philippe\.platformio\packages\framework-arduinoespressif8266\libraries\ESP8266WiFi)
|-- <MqttHandler> (C:\development\platformio\morse-project\lib\MqttHandler)
Building in release mode
[...]

Do you have any idea what’s going wrong?

Why is there a library.json in the lib/ folder and not in the MyLib folder? What’s the content of this very critical file? What’s the platformio.ini?

1 Like

Hi maxgerhardt,

oh, thanks for pointing that out - actually the library.json is placed within the MyLib folder. It has just the wrong indentation in my first post. So here’s the correct structure:

MyProject/
   lib/
      MyLib/
         src/
            MyLib.h
            MyLib.cpp
         library.json
   src/
      main.cpp
   platformio.ini

And here’s the content of the library.json:

{
  "dependencies": [
    {
      "name": "PubSubClient"
    }
  ]
}

The platformio.ini in the root project:

[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2    
framework = arduino

Is anything wrong with that?

1 Like

Maybe it doesn’t like it when you specify the library without a version number. Try using the documentated short form.

Also you’re not putting in any of the values which are marked as required in the doc – your library must have at least a name, version, description, keywords and repository in the library.json.

Try with a library.json like:

{
  "name": "MyLib",
  "description": "My test library",
  "keywords": "keyword1,keyword2",
  "version": "1.0.0",
  "repository":
  {
      "type": "git",
      "url": "<your actual .git link here>"
  },
  "dependencies": 
  {
     "PubSubClient": "^2.8"
  }
}
1 Like