PlatformIO Community

Including nested libraries

Hi,
I have written a lot of custom libraries which are in the /lib folder so they can be unit tested (so don’t need to use test_build_project_src = true.

I have enough that I want to start moving them into subdirectories so:

/lib
   - /WirelessComms
       - /ServerCommunication
            - ServerCommunication.h
            - ServerCommunication.cpp
       - /ClientCommunication
            - ClientCommunication.h
            - ClientCommunication.cpp
   - /Interfaces
       - /RS485
            - .....
       - /CANBus
            - .....
/src
    - main.cpp
   - ....etc

When the libraries are not in the Interfaces and WirelessCommunication folders, they are included fine by #include "ClientCommunication.h", but when I move them to the above folder structure and try to include them by #include "WirelessComms/ClientCommunication.h" they aren’t found.

I have tried chain and deep LDF modes but neither seem to work.

Any thoughts on why they aren’t being found?

1 Like

If the folder structure is then lib -> WirelessComms -> ClientCommunication.h, the header should be included as just #include <ClientCommunication.h>, since the WirelessComms library is then in the include path.

Does the dependency graph show that the libraries are included then?

Hi Max,

Unfortunately that doesn’t seem to have done it. Both quotation include and <> include doesn’t seem to find it and neither the WirelessComms folder or ClientCommunication header is in the dependency graph.

pio run -v | grep "ClientCommunication" also doesn’t give anything

What is the full platformio.ini for reproduction?

[platformio]
default_envs = esp8266, esp32

[env] ;Common is done for all environments
monitor_speed = 115200
monitor_flags= --raw

lib_extra_dirs =
  ./shared-config

lib_deps =
  bblanchon/ArduinoJson @ ^6.17.2
  adafruit/Adafruit ADS1X15 @ ^1.1.1
  adafruit/Adafruit MCP4728 @ ^1.0.7
  adafruit/Adafruit NeoPixel @ ^1.7.0
  arduino-libraries/NTPClient @ ^3.1.0

test_build_project_src = true ;The test protocol will build the contents of the src folder when running tests. Allows us to test functions inside src

; ==== STATIC CHECKING ====
check_tool = cppcheck ; pvs-studio, cppcheck, clangtidy
check_flags = 
  cppcheck: --enable=all
  cppcheck: --inline-suppr
  cppcheck: --suppress=*:.pio/libdeps/*
check_severity = medium, high
check_patterns = 
  src
  lib
  include

[extra]
extra_scripts_default =
  shared/moveOutputBinaries.py

build_flags_default =
  -DDEBUG_ESP_PORT=Serial
  -DDEBUG_BUILD
  -DWEBSOCKET_DISABLED
  !python shared/GetCurrentDateTime.py
  !python shared/GetGitHash.py
  ; -DDEBUG_ESP_OOM ;https://docs.platformio.org/en/latest/platforms/espressif8266.html#debug-level
  ; -Wl,-Map,output.map ;Creates a map file to figure out relative sizes of included stuff
;  -DDEBUG_DISABLED ;Disables the Remote Debug - Telnet debugging

;For when debugging
; build_unflags = -Os
; build_flags = -O1 -g3 -ggdb

;==========ESP32===========
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
build_type = debug
debug_tool = esp-prog
; monitor_filters = esp32_exception_decoder
build_flags =
  ${extra.build_flags_default}
extra_scripts = ${extra.extra_scripts_default}
debug_init_break = tbreak setup

;========ESP8266===========
[env:esp8266]
platform = espressif8266
board = nodemcu
framework = arduino
build_type = debug
board_build.ldscript = eagle.flash.4m1m.ld ;Sets the flash sizes properly so SPIFFS is last 1MB starting at 0x30000
extra_scripts = ${extra.extra_scripts_default}
build_flags =
  ${extra.build_flags_default}

[env:native_unit_testing]
platform = native
build_type = debug
test_build_project_src = true ;The test protocol will build the contents of the src folder when running tests. Allows us to test functions inside src
build_flags =
  ${extra.build_flags_default}
  -I"include/unit_testing" ;Includes Arduino.h for native builds
  ; -Wall -fprofile-arcs -ftest-coverage -lgcov --coverage

Is there any more verbose output that I can see for the building of the dependency list or how the include list is made?

I would also like the format #include "[Category]/[header].h" if possible for cleaner looking code and also making it easier to reverse find the library files.

Does anyone else have any ideas? I am thinking I can have a script that will run and modify the include path to allow for the nested nature or else doing

lib_extra_dirs = ./lib

Should cause them to allow nesting but I’m not a fan of doing it that way.

I’m have the same approach as yours, and the same #include problem.
I am looking for a solution to manually include all libraries, io. LDF trying to do that in an automatic way.

I’m afraid I just went with the
lib_extra_dirs = ./lib/sub_dir
approach.

Which does mean that the include headers don’t reflect the folder structure and organisation that I was hoping for (and being able to refer to header files with the same name in different folders) but at least it compiles