PlatformIO Community

Compiling cpp files of nested libraries

Hello,

apologies if this has been asked before but after spending 4h looking for a solution I figured I will ask this quick question.

I am creating a library that depends on several other libraries which are placed in the lib folder of the root library. Every library has a library.json. The root library has build flags defined in the library.json which include the src directory of each of the nested libraries using -I pathToSrc.

The include paths are working nicely but unfortunately, the linker fails with undefined references i.e. my .cpp files (located in the src directory) are not compiled.

I know that if I specify each of the library src folders with the srcFilter in the root library.json they will compile but I have several srcFilters in the nested libraries that are important for their functioning. Unfortunately, it seems that the srcFilter of the root library will overwrite the srcFilter of the nested library which means I would need to specify the full path of all the srcFilter directives of each nested library in the root library which is a very ugly way of doing this.

What is the proper way of doing this? Is it possible to tell the root library.json to use the srcFilter of the nested library to find my folders?

Thanks a lot!

No, if a library depends on a certain other library, PlatformIO’s LDF should identify it and include it in the build process. Then, the libary is automatically in the include path. No need for -I flags.

You might need to increase the lib_ldf_mode if the library is not being identified.

Otherwise, please show a minimal example with your folder structure.

thanks for the response.
here is my folder structure of the whole project

include/
lib/
├─ rootLibrary/
│  ├─ lib/
│  │  ├─ nestedLibrary1/
│  │  │  ├─ src/
│  │  │  ├─ library.json
│  │  ├─ nestedLibrary2/
│  │  │  ├─ src/
│  │  │  ├─ library.json
│  ├─ src/
│  ├─ library.json
src/
platformio.ini

Here is my current library.json

{
  "name": "rootLibrary",
  "version": "1.0.0",
  "description": "bla",
  "authors": [        {          "name": "me"        }      ],
  "build": {
    "flags": ["-D ARDUINO=100",
      "-I lib/nestedLibrary1/src",
      "-I lib/nestedLibrary2/src",
      "-I src/DecisionTree/Action",
      "-I src/DecisionTree/Condition"
      ],
    "srcFilter": [
      "+<DecisionTree/Action/*>",
      "+<DecisionTree/Condition/*>",
      "-<main.cpp>"]
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/my/rootlibrary.git"
  },
  "frameworks": ["espidf", "arduino"],
  "platforms": "espressif32",
  "dependencies": [
    {
      "name": "arduino-esp32",
      "version": "https://github.com/ArdenVent/arduino-esp32.git#idf-release/v4.0"
    }
  ]
}

and here I tried to declare the other libraries as dependencies instead of -I but it won’t find them at all this way

{
  "name": "rootLibrary",
  "version": "1.0.0",
  "description": "Bla",
  "authors": [    {      "name": "me"    }  ],
  "build": {
    "flags": ["-D ARDUINO=100",
      "-I src/DecisionTree/Action",
      "-I src/DecisionTree/Condition"
      ],
    "srcFilter": [
      "+<DecisionTree/Action/*>",
      "+<DecisionTree/Condition/*>",
      "-<main.cpp>"],
    "libLDFMode": "deep+"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/myRootLibrary.git"
  },
  "frameworks": ["espidf", "arduino"],
  "platforms": "espressif32",
  "dependencies": [
    {
      "name": "arduino-esp32",
      "version": "https://github.com/ArdenVent/arduino-esp32.git#idf-release/v4.0"
    },
    {"name":"nestedLibrary1"},
    {"name":"nestedLibrary2"},
   
  ]
}

This source folder arrengement is not possible in PlatformIO, because it detects rootLibrary/src as the source directory, the sources in rootLibrary/lib/ will never be compiled.

Just pull out the nestedLibrary1 into the main lib/ folder of the project, as their own libraries. The LDF will pick up the dependency to them.

thanks I will give that a try. Is it possible to download them manually somehow? maybe using the library.json file? I want to reuse the library in multiple projects and there are like 8 nested libraries. It would be a lot nicer if that can all be done in one go.

Another thing that would work for me is if I could use glob patterns. The documentation states that it is possible to use these, however trying to filter out all main.cpp files using "-<../lib/*/main.cpp>" does not work. Are there some limitations to the glob pattern usage that are not documented?

edit nevermind, I realized I need to use "-<../lib/**/main.cpp>". This solves my issue. For anybody looking for a similar solution, I ended up putting everything into the lib directory as shown above and added the srcFilter "+<../lib/>", "-<../**/examples/>" and I further created a header file for each of my nested libraries in the root library src folder which includes the actual header file from the lib folder to shut up intellisense which would not utilize the library.json it seems