Page 1 of 1

Including files in one custom component from another

Posted: Tue Feb 19, 2019 12:48 am
by eagi223
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:
  1. COMPONENTS_SRCDIRS := ComponentA/src
  2. 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

Re: Including files in one custom component from another

Posted: Tue Feb 19, 2019 1:13 am
by ESP_igrr
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".

Re: Including files in one custom component from another

Posted: Tue Feb 19, 2019 5:22 pm
by eagi223
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.

Re: Including files in one custom component from another

Posted: Wed Feb 20, 2019 1:23 pm
by Denis Brion
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.

Re: Including files in one custom component from another

Posted: Thu May 02, 2019 5:14 pm
by eagi223
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.

Re: Including files in one custom component from another

Posted: Thu Mar 04, 2021 1:00 pm
by ksnboopa
I have come across the same issue, got it sorted using this video.
It may help anyone https://youtu.be/RAdvZw00wRk