Search code examples
cmakeesp32esp-idfespressif-idf

CMAKE & ESP IDF: how to include components as subfolders?


My project looks like this,

.components
...drivers
......usb
........usb.c
........usb.h
......lcd
......etc
.main
...main.c

In main.c, I would like to include libraries as

#include "drivers/usb.h"
#include "drivers/lcd.h"

How to configure CMake to include subfolders, but include component itself as a folder?

I have tried to include subfolders as PRIV_INCLUDE_DIRS, but it works like this,

#include "usb/usb.h" 

instead of

#include "component/sourcefile.h"

esp-idf components like driver and FreeRTOS manage to achive this so well, just can't figure out how to implement such style.

Any help appreciated!


Solution

  • First of all, if you want to use a header file outside the component itself, you need to use INCLUDE_DIRS instead of PRIV_INCLUDE_DIRS when calling idf_component_register(...) in the component's CMakeLists.txt. For more information, see the IDF Build System component commands documentation page.

    To enable including header files as driver/xxx.h, you can do the following: Add a sub-folder driver in each of usb, lcd, etc., move the respective header files there and add each of the directories (usb, lcd, etc.), using INCLUDE_DIRS in idf_component_register(...). Your file tree would look like:

    .components
    ...drivers
    ......usb
    ........usb.c
    ........driver
    ..........usb.h
    ......lcd
    ........driver
    ..........lcd.h (not present above, but requested in your code example)
    ......etc
    .main
    ...main.c
    

    This is in fact done for many driver headers in ESP-IDF itself.