Page 1 of 1

Call component function from another component

Posted: Sun Jan 24, 2021 5:16 pm
by tsctrl
Hi,

I am having difficulties to call a function in a component from another component. Is it possible to call function in a component from another component? i have use static, extern, pointer call but all getting the same error.
I am able to call the component in project main files but when i call from other component i am getting :

Code: Select all

undefined reference to B::init(). in function `A::init()'
example:
component A.h:

Code: Select all

class A
{
public:
	A(){};
       static init();
}
component B.h:

Code: Select all

class B
{
       B(){};
       static init();
}
component A.cpp:

Code: Select all

B b;
init(){
     b.init(); // error
     or
     B::init(); // error
}
Basically i want to have the component function as static and able to call it in any files from other components.
Thank you.

Re: Call component function from another component

Posted: Mon Jan 25, 2021 6:37 am
by RichPiano
You must set the REQUIRES or PRIV_REQUIRES fields in the CMakeLists.txt of the components, otherwise the linker doesn't "see" the other components (they don't automatically require each other).

Re: Call component function from another component

Posted: Mon Jan 25, 2021 1:00 pm
by tsctrl
Hi, @richpiano thank you for your reply. i have added component B in the A REQUIRES CMakeList.txt. also added in the main CMakeList.txt as required.

for missing REQUIRES the error will be "No such file or Directory" error in the header include.

Thank you.

Re: Call component function from another component

Posted: Tue Jan 26, 2021 1:31 pm
by tsctrl
Can someone from Espressif ask Renz on this one?

Thank you!

Re: Call component function from another component

Posted: Wed Jan 27, 2021 12:55 pm
by ESP_renz
for missing REQUIRES the error will be "No such file or Directory" error in the header include.
You can check if the directory that contains the missing header is specified in the `INCLUDE_DIRS` argument to `idf_component_register` of the component that contains it.

It might also be possible that this directory is specified in the component's `PRIV_INCLUDE_DIRS` argument, in this case other components will not be able to see it.

Re: Call component function from another component

Posted: Wed Jan 27, 2021 2:05 pm
by tsctrl
Hi Renz,

Thank for the reply. but i already did that. The error is not "No such file or Directory" error but is "undefined reference to B::init(). in function `A::init()'" error.

the way i did is to add this code to have it to work:

Code: Select all

target_sources(${COMPONENT_LIB} PRIVATE ${COMPONENT_SRCS})
i not sure what this code is doing. but still adding the code above only solving if i have only one component called by another component.
if i have two component function called in a component i still have the same undefined reference issue. There is no exact error just the compiler say this A::init() function is undefine. I guess i should write all my code in c instead.

Thank you.