Components and CMakeList

ArminArmin
Posts: 9
Joined: Mon Aug 08, 2022 6:14 pm

Components and CMakeList

Postby ArminArmin » Mon Dec 05, 2022 7:46 pm

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:

Code: Select all

#include "dd.h"
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

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: Components and CMakeList

Postby ESP_igrr » Tue Dec 06, 2022 7:41 am

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?

ArminArmin
Posts: 9
Joined: Mon Aug 08, 2022 6:14 pm

Re: Components and CMakeList

Postby ArminArmin » Tue Dec 06, 2022 6:27 pm

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

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: Components and CMakeList

Postby ESP_igrr » Wed Dec 07, 2022 10:35 am

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.

ArminArmin
Posts: 9
Joined: Mon Aug 08, 2022 6:14 pm

Re: Components and CMakeList

Postby ArminArmin » Thu Dec 08, 2022 1:37 pm

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?


ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: Components and CMakeList

Postby ESP_igrr » Tue Dec 13, 2022 4:04 pm

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.

Who is online

Users browsing this forum: No registered users and 127 guests