Call component function from another component

tsctrl
Posts: 30
Joined: Tue Dec 08, 2020 1:37 pm

Call component function from another component

Postby tsctrl » Sun Jan 24, 2021 5:16 pm

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.

RichPiano
Posts: 123
Joined: Mon May 18, 2020 2:51 pm

Re: Call component function from another component

Postby RichPiano » Mon Jan 25, 2021 6:37 am

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).

tsctrl
Posts: 30
Joined: Tue Dec 08, 2020 1:37 pm

Re: Call component function from another component

Postby tsctrl » Mon Jan 25, 2021 1:00 pm

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.

tsctrl
Posts: 30
Joined: Tue Dec 08, 2020 1:37 pm

Re: Call component function from another component

Postby tsctrl » Tue Jan 26, 2021 1:31 pm

Can someone from Espressif ask Renz on this one?

Thank you!

ESP_renz
Posts: 18
Joined: Tue May 14, 2019 2:41 am

Re: Call component function from another component

Postby ESP_renz » Wed Jan 27, 2021 12:55 pm

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.

tsctrl
Posts: 30
Joined: Tue Dec 08, 2020 1:37 pm

Re: Call component function from another component

Postby tsctrl » Wed Jan 27, 2021 2:05 pm

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.

Who is online

Users browsing this forum: tharanilc and 228 guests