How to fix ESP-IDF component error: #include <driver/...>: No such file or directory

Problem

When compiling your ESP-IDF project, which contains a custom component, you may encounter the following error:

In file included from /home/uli/myproject/components/QuickI2C/QuickI2C/src/QuickI2C.cpp:1:
/home/uli/myproject/components/QuickI2C/QuickI2C/include/QuickI2C.h:21:10: fatal error: driver/i2c.h: No such file or directory
   21 | #include <driver/i2c.h>
      |          ^~~~~~~~~~~~~~
compilation terminated.

Solution

Edit the CMakeLists.txt file of your custom component and add the following statement inside idf_component_register:

REQUIRES driver

Exmple CMakeLists.txt

idf_component_register(SRCS "QuickI2C/src/QuickI2C.cpp"
                    INCLUDE_DIRS "QuickI2C/include"
                    REQUIRES driver)