Search code examples
cincludeesp32esp-idf

Can certain header files only be included from main file?


I am developing a project for the ESP32 using ESP-IDF. I am trying to use the Bluetooth libraries inside a component, but the compiler complains that the header file does not exist. Including the same header file from main.c works without any problems. The header files I'm trying to include are:

#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gatts_api.h"
#include "esp_gap_ble_api.h"
#include "esp_gatt_common_api.h"

Is there any reason why there are certain header files that can only be included from main.c? Is there any solution or does one have to put certain functionality in the main.c file to appease ESP-IDF?

Note: I have the same problem with including header files when trying to use HTTPS Server.

In case it's of any relevance this is the project structure I'm using:

- CMakeLists.txt
- Makefile
- sdkconfig
- main
    - CMakeLists.txt
    - component.mk
    - main.c
- components
    - component1
        - component1.h
        - component1.c
        - CMakeLists.txt

The CMakeLists.txt files are all identical:

idf_component_register(SRCS "ble.c" INCLUDE_DIRS ".")

The main function is a very simple function that sets up the environment (eg. NVS_FLASH) and calls each component's setup function and terminates, leaving control of the program to the components.


Solution

  • You should definitely be able to include the files from any place. Adding a REQUIRES <component-name> to your idf_component_register command should solve the problem: idf_component_register(SRCS "ble.c" INCLUDE_DIRS "." REQUIRES bt).

    This is the general way in IDF to include other components.