Including nested libraries

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:

   - /WirelessComms
       - /ServerCommunication
            - ServerCommunication.h
            - ServerCommunication.cpp
       - /ClientCommunication
            - ClientCommunication.h
            - ClientCommunication.cpp
   - /Interfaces
       - /RS485
            - .....
       - /CANBus
            - .....
    - 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?


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?

default_envs = esp8266, esp32

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

lib_extra_dirs =

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

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 = 

extra_scripts_default =

build_flags_default =
  !python shared/
  !python shared/
  ; -Wl,-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

platform = espressif32
board = esp32dev
framework = arduino
build_type = debug
debug_tool = esp-prog
; monitor_filters = esp32_exception_decoder
build_flags =
extra_scripts = ${extra.extra_scripts_default}
debug_init_break = tbreak setup

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 =

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 =
  -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

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