Including files in one custom component from another

eagi223
Posts: 16
Joined: Fri Mar 02, 2018 4:46 pm

Including files in one custom component from another

Postby eagi223 » Tue Feb 19, 2019 12:48 am

I'm working on a few projects that share a fair amount of code. I'm trying to modularize this code into custom components for use in the various IDF projects I have to make it easier to maintain the common code. The issue I'm having is that in ComponentA I haven't been able to find a way to include header files from ComponentB (besides using the full relative path). I'm sure its doable since the IDF components do the exact thing I'm wanting to do (eg: other ESP-IDF components can include esp_log.h). I'm guessing either something is wrong in my include path or my component.mk files.

I have the following file structure:
project
\-build
\-components
\-ComponentA
|-component.mk
\-ComponentA
\-include
|-componentA.h
\-src
|-componentA.c
\-ComponentB
|-component.mk
\-ComponentB
\-include
|-componentB.h
\-src
|-componentB.c
\-main
|-main.c
|-component.mk
|-Makefile
|-partition.csv
|-sdkconfig

So basically my components directory has two components in it. ComponentA and ComponentB containing another directory and a component.mk file. Each of the sub directories have another directory containing the src and include directories.

My component.mk files look like:

Code: Untitled.make Select all


COMPONENTS_SRCDIRS := ComponentA/src
COMPONENT_ADD_INCLUDEDIRS := ComponentA/include
I can use the components themselves fine as long as I use the entire relative path, but it won't compile if I try using #include "ComponentA/include/componentA.h" or "ComponentA/componentA.h" or "ComponentA/ComponentA/include/componentA.h" or "components/ComponentA/ComponentA/include/componentA.h". Make throws "fatal error: {path I used in include}: No such file or directory. I really need ComponentA to depend on .h files from ComponentB.

Does anyone have any ideas as to what I'm doing wrong here?

Thanks in advance!
Ethan

igrr
Espressif staff
Espressif staff
Posts: 2077
Joined: Tue Dec 01, 2015 8:37 am

Re: Including files in one custom component from another

Postby igrr » Tue Feb 19, 2019 1:13 am

Does it work if you include "componentA.h" (as your include path is already set to ComponentA/include)?

If not, please post the output of "make V=1".

eagi223
Posts: 16
Joined: Fri Mar 02, 2018 4:46 pm

Re: Including files in one custom component from another

Postby eagi223 » Tue Feb 19, 2019 5:22 pm

Thanks for the response! So I ran "make V=1" and changed my includes to just the name of the file. With the output of make V=1 I was able to track down the issue. It turns out that my ComponentA makefile was NOT setting the include path to ComponentA/include... So I'm not sure what the default component.mk behavior is if it is given a bad path for the include path. I don't know if it sets the invalid path or defaults to just setting it to include or what. That was the issue though. I'm sorry, it was a dumb oversight on my part. Thanks though! make V=1 is what helped me track that down.

Denis Brion
Posts: 9
Joined: Tue Feb 19, 2019 4:45 pm

Re: Including files in one custom component from another

Postby Denis Brion » Wed Feb 20, 2019 1:23 pm

Excuse me, but if you are under linux/Unix,

Code: Select all

 ln -s
can be tryied (shares (contents of) a file between two directories (file names): if you modify one version of the file, say in directory A, it is modified in the other directory and no extra room (except for a "adress" ) is used.... This can be used as a drawback or as comfortable.

eagi223
Posts: 16
Joined: Fri Mar 02, 2018 4:46 pm

Re: Including files in one custom component from another

Postby eagi223 » Thu May 02, 2019 5:14 pm

I was more looking for how to link components (compilation-wise via their component.mk files) so that one component can include a library from another component, but I figured it out as shown above. It was just a path typo in my component.mk file.

ksnboopa
Posts: 9
Joined: Sun May 03, 2020 3:19 pm

Re: Including files in one custom component from another

Postby ksnboopa » Thu Mar 04, 2021 1:00 pm

I have come across the same issue, got it sorted using this video.
It may help anyone https://youtu.be/RAdvZw00wRk

Who is online

Users browsing this forum: Amazon [Bot], Qwantbot, Semrush [Bot] and 1 guest