Referencing of component only works with cyclic dependency CMAKE

AG_OHP
Posts: 9
Joined: Mon Oct 10, 2022 7:28 am

Referencing of component only works with cyclic dependency CMAKE

Postby AG_OHP » Fri Feb 24, 2023 12:58 pm

Hi i have a problem, i have 2 components. "ComponentA" controls pin output. It defines functions for this. The idea is,that the other component "ComponentB" calls this function and can set the pins.

ComponentB needs these functions. They call the componentA_function in its own functions. So i included the headers, and put "requires componentA" in the cmake. However, during linking the build terminal then tells me "undefined reference to function"

There were 2 ways to make the programm link:

1. Call the ComponentA_function(s) in main_app. After that, the call in ComponentB was not undefined in the linking step.
2. Introduce a dependency to componentB in the cmakelists.txt of componentA. However, this should NOT be necessary. ComponentA does not use a single line of code of componentB. So why does the code only compile if set "componentB" as REQUIRED in the cmakelists.txt of "componentA". I don't like introducing unnecessary cyclic redundancies i hope you understand. Especially if i dont see a reason for them, in a month or two I will not understand the dependency anymore if i want to debug anything.. I do not want to include a errorsource i cannot find later on.

Do you know similar errors? Is there a common mistake i did?

Thanks in advance for your help.

Below i List the Project structure and the different cmake files
  1. components
  2. \__componentA
  3.      \__include
  4.           \__componentA.h
  5.      \__CmakeLists.txt
  6.      \__componentA.c
  7.  
  8. \__componentB
  9.      \__include
  10.           \__componentB_0.h //calls functions of componentB_1.h
  11.           \__componentB_1.h //calls functions of componentB_2.h
  12.           \__componentB_2.h //calls functions of componentA.h
  13.      \__CmakeLists.txt
  14.      \__componentB_0.c
  15.      \__componentB_1.c
  16.      \__componentB_2.c
  17.  
  18. main
  19. \__CmakeLists.txt
  20. \__main.c
  21. CmakeLists.txt
//////////////CmakeLists.text of ComponentA <----- PROBLEM
/////////////////////////////////////////
  1. idf_component_register(SRCS "componentA.c"
  2.                        INCLUDE_DIRS "include"
  3.                        REQUIRES driver freertos nvs_flash "main" "componentB" log spi_flash)<----- PROBLEM
  4. #"componentB" should NOT be a dependency, since componentB only calls componentA functions, <----- PROBLEM
  5. #but if you dont include "componentB" here, the componentA reference to the functions is undefined.
  6.  
  7.  
  8. include($ENV{IDF_PATH}/tools/cmake/project.cmake)
//////////////CmakeLists.text of ComponentB
/////////////////////////////////////////
  1. idf_component_register(SRCS "componentB_0.c" "componentB_1.c" "componentB_2.c"
  2.                     INCLUDE_DIRS "include"
  3.                     REQUIRES  "main" "componentA " "SPI" freertos
  4.                     )
  5.  
  6. include($ENV{IDF_PATH}/tools/cmake/project.cmake)
//////////////CmakeLists.text of main
/////////////////////////////////////////
  1. idf_build_get_property(project_dir PROJECT_DIR)
  2. idf_component_register(SRCS "native_ota_example.c"
  3.                     INCLUDE_DIRS "."
  4.                     )
//////////////CmakeLists.text toplevel
/////////////////////////////////////////
  1. # The following lines of boilerplate have to be in your project's CMakeLists
  2. # in this exact order for cmake to work correctly
  3. cmake_minimum_required(VERSION 3.16)
  4.  
  5. # (Not part of the boilerplate)
  6. # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
  7. set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
  8.  
  9. include($ENV{IDF_PATH}/tools/cmake/project.cmake)
  10. project(native_ota)

MicroController
Posts: 1706
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Referencing of component only works with cyclic dependency CMAKE

Postby MicroController » Sun Feb 26, 2023 8:10 pm

Your "main" CmakeLists.txt should declare a dependency to your component(s), i.e.

Code: Select all

idf_component_register(SRCS "native_ota_example.c"
	INCLUDE_DIRS "."
	REQUIRES "componentB"
)
This should pull componentB into the build, and from there transitively componentB's dependencies a.k.a. componentA.

AG_OHP
Posts: 9
Joined: Mon Oct 10, 2022 7:28 am

Re: Referencing of component only works with cyclic dependency CMAKE

Postby AG_OHP » Fri Nov 08, 2024 11:59 am

I just saw that i never replied.

Thank you. It worked.

Who is online

Users browsing this forum: No registered users and 138 guests