Prebuilt library one level above root

Hello,

I would like to include pre-built component library in my project, but directory level is required to be one level above the project’s root directory containing the platformio.ini. But it returns error saying that "undefined reference to `init_iaq_2nd_gen’ ".
Please see structure below.

  • components
    • zmod4xxx (component directory)
  • project_root
    • platformio.ini
    • src
      My platformio.ini looks like this.
      [env:esp32-s3-devkitc-1]
      platform = espressif32
      board = esp32-s3-devkitc-1
      framework = espidf

monitor_speed = 115200
lib_extra_dirs =
…/components

I tried to debug with the message,
message(“Linking ${blob} library: ${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}/lib_${blob}.a”).
The message output confirms that the path is correct and the prebuilt library is being linked from the expected location.
Please note that if I include components folder with same level as project root then it compiles without any issue.

Please advise me.

Thank you
Lhagva

So does the linker command contain a wrong -L path to the components/zmod4xxx folder then or a wrong -l<library name> flag? Check the full invocation with project tasks → Advanced → Verbose Build.

@maxgerhardt

Thank you for reply.
I tried Advanced → Verbose Build.
But it returns same error.

Dependency Graph
|-- sensor (License: Unknown, Path: /home/lhagva/Documents/PlatformIO/Projects/iaq_zmod4xxx/common/sensor)
|-- wifi (License: Unknown, Path: /home/lhagva/Documents/PlatformIO/Projects/iaq_zmod4xxx/common/wifi)
|-- zmod4xxx (License: Unknown, Path: /home/lhagva/Documents/PlatformIO/Projects/iaq_zmod4xxx/components/zmod4xxx)
|   |-- i2cdev (License: Unknown, Path: /home/lhagva/Documents/PlatformIO/Projects/common/esp-idf-lib/components/i2cdev)
|   |   |-- esp_idf_lib_helpers (License: Unknown, Path: /home/lhagva/Documents/PlatformIO/Projects/common/esp-idf-lib/components/esp_idf_lib_helpers)
|-- bmp280 (License: Unknown, Path: /home/lhagva/Documents/PlatformIO/Projects/common/esp-idf-lib/components/bmp280)
|   |-- i2cdev (License: Unknown, Path: /home/lhagva/Documents/PlatformIO/Projects/common/esp-idf-lib/components/i2cdev)
|   |   |-- esp_idf_lib_helpers (License: Unknown, Path: /home/lhagva/Documents/PlatformIO/Projects/common/esp-idf-lib/components/esp_idf_lib_helpers)
|   |-- esp_idf_lib_helpers (License: Unknown, Path: /home/lhagva/Documents/PlatformIO/Projects/common/esp-idf-lib/components/esp_idf_lib_helpers)
Building in release mode
...
/home/lhagva/.platformio/packages/toolchain-xtensa-esp32s3/bin/../lib/gcc/xtensa-esp32s3-elf/12.2.0/../../../../xtensa-esp32s3-elf/bin/ld: .pio/build/esp32-s3-devkitc-1/src/sesub.o:(.literal.init_zmod4xxx+0xc0): undefined reference to `init_iaq_2nd_gen'

For further investigation please see complete CMakeLists.txt below.

idf_build_get_property(idf_target IDF_TARGET)

idf_component_register(
    # sources to compile
    SRCS src/zmod4xxx.c HAL/zmod4xxx_hal.c
    INCLUDE_DIRS include HAL lib/esp32s3
    REQUIRES driver i2cdev log
)

idf_build_get_property(build_dir BUILD_DIR)

set(target_name "${idf_target}")
# This sets the link directory for the COMPONENT_LIB. 
# It's specifying a directory where linker libraries for the component will be found
target_link_directories(${COMPONENT_LIB} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}")

# adding a prebuilt library for iaq_2nd_gen
set(blob iaq_2nd_gen)
add_prebuilt_library(${blob} "${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}/lib_${blob}.a"
                        REQUIRES ${COMPONENT_NAME})

# This links the ${blob} library to the COMPONENT_LIB
message("Linking ${blob} library: ${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}/lib_${blob}.a")
target_link_libraries(${COMPONENT_LIB} PUBLIC ${blob})

# adding a prebuilt library for zmod4xxx_cleaning
set(blobcleaning zmod4xxx_cleaning)
add_prebuilt_library(${blobcleaning} "${CMAKE_CURRENT_SOURCE_DIR}/lib/${target_name}/lib_${blobcleaning}.a"
                        REQUIRES ${COMPONENT_NAME})

# This links the ${blob1} library to the COMPONENT_LIB
target_link_libraries(${COMPONENT_LIB} PUBLIC ${blobcleaning})

target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")

I’m a bit confused, a CMakeLists.txt is for ESP-IDF components, but lib_extra_dirs = ... is for the PlatformIO library system. A library in the PlatformIO library system is added to the build per its library.json specifications (if such a file exists at all), not according to the CMakeLists.txt, that would be ignored. Specifically, libaries added via lib_deps/lib_extra_dirs are not evaluated as ESP-IDF components by the CMake build system (see issue).

I’d suggest removing the lib_extra_dirs line from the platformio.ini and only going through the CMake build system by declaring ../components as a to-be-added extra component dir using

list(APPEND EXTRA_COMPONENT_DIRS ../components)

in the CMakeLists.txt at the root of the project, just like the reference example.

@maxgerhardt Thank you for clarification.
I removed …/components from platformio.ini, but I keep other needed folders in platformio.ini as below.
lib_extra_dirs =
…/…/common/esp-idf-lib/components
…/…/common/components
…/common
Is it correct? or if once I use CMake build system, then shall I remove all of them?

Because it seems not building zmod4xxx at all.

Thank you

@maxgerhardt Please note that I added extra component dir as below.
cmake_minimum_required(VERSION 3.16.0)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
list(APPEND EXTRA_COMPONENT_DIRS …/components)

project(esp32s3_mqtt_ex)

@maxgerhardt
Hello, Please confirm if I declare extra component dir using
list(APPEND EXTRA_COMPONENT_DIRS ../components), then I can not use other components which defined in the lib_deps/lib_extra_dirs of platformio.ini.
I mean is it possible to use two different components (one is declared in the CMakeLists.txt, and another from platformio.ini lib_deps /lib_extra_dirs) simultaneously in one project.
Thank you and best regards
Lhagva

I think that’s possible as normal. If you have problem → Issue at https://github.com/platformio/platform-espressif32/issues/

@maxgerhardt I have submitted an issue and got the fix in the upstream version.
Thank you for your kind advice.