Page 1 of 1
Components and CMakeList
Posted: Mon Dec 05, 2022 7:46 pm
by ArminArmin
Hi,
I have been suggested to put different libraries into a components folder and address them using CMakeList.txt .
My folder structure lookslike this:
Code: Select all
- main
main.c
- components
-Lib_A
aa.c
aa.h
- A1
bb.c
bb.h
_ A2
dd.c
dd.h
-Lib_B
ee.c
ee.h
- B1
ff.c
ff.h
- B2
gg.c
gg.h
I put this in the CMakeList:
Code: Select all
set(EXTRA_COMPONENT_DIRS "components")
Then I included it with:
But idf shows:
Code: Select all
fatal error: dd.h: No such file or directory
I read here also:
https://docs.espressif.com/projects/esp ... ystem.html
What is the solution?
Thanks
Re: Components and CMakeList
Posted: Tue Dec 06, 2022 7:41 am
by ESP_igrr
Hi Armin,
ArminArmin wrote:
I put this in the CMakeList:
CODE: SELECT ALL
set(EXTRA_COMPONENT_DIRS "components")
This part is not necessary, "components" directory of the project is searched for components by default.
There can be two reasons for this error. Either you haven't added the directory A2 as a public include directory of component Lib_A; or you didn't add Lib_A component as a dependency to the component where you have '#include" dd.h"'.
Could you please post the CMakeLists.txt file of the component Lib_A and of the component where the "include" error happens?
Re: Components and CMakeList
Posted: Tue Dec 06, 2022 6:27 pm
by ArminArmin
Thanks,
I got only two CMakeList files, one is in the project folder and the other one is in the main/ folder.
Like this:
Code: Select all
project folder:
- components
- main
-CMakeLists.txt
- CMakeLists.txt
Re: Components and CMakeList
Posted: Wed Dec 07, 2022 10:35 am
by ESP_igrr
Okay, that might be the issue then.
Each component in the ESP-IDF build system should have a CMakeLists.txt file. You can find more information about component CMakeLists.txt files at
https://docs.espressif.com/projects/esp ... ists-files. Please try creating the CMakeLists.txt files in the components Lib_A and Lib_B.
For example, based on the structure you have shown, the CMakeLists.txt for Lib_A might look as follows:
Code: Select all
idf_component_register(SRCS aa.c A1/bb.c A2/dd.c
INCLUDE_DIRS . A1 A2
REQUIRES driver esp_wifi
)
This will do the following:
- Compile source files aa.c, A1/bb.c and A2/dd.c
- Expose the component root dir (.) and A1, A2 as public include directories of the component. This will allow other components to use `#include "aa.h"` or `#include "bb.h"`
- Specify which other components Lib_A depends on. In this example I have put "driver" and "esp_wifi" there. You need to change it based on the header files you want to include in Lib_A.
Re: Components and CMakeList
Posted: Thu Dec 08, 2022 1:37 pm
by ArminArmin
Thank you so much for explaining.
Out of curiosity,
If A1/ directory has multiple files (.h), then each of those files should be written in the CMakeList?
For example, Let's say there are 100 files in the A1/ , then all of those 100 files should be specified in the CMakeList?
Is it possible to specify the folder name only?
Re: Components and CMakeList
Posted: Sun Dec 11, 2022 11:57 pm
by chegewara
Re: Components and CMakeList
Posted: Tue Dec 13, 2022 4:04 pm
by ESP_igrr
In addition to the options mentioned by chegewara, idf_component_register also accepts SRC_DIRS argument. You can specify the names of the directories, and IDF will look for .c, .cpp, .S source files in those directories. This argument is mentioned in the same build-system reference page linked earlier.
Unlike source files, header files do not need to be specified in CMake.