Search code examples
cmakemqttesp32esp-idfcmakelists-options

CMake and ESP-IDF: Creating custom components


Framework:

ESP-IDF v4.3.1 - Eclipse Plugin

Project with 1 active component (my plan is to add ble scanning, mqtt messaging and wifi access)

IDF_PATH:        C:\Users\username\source\esp-idf-v4.3.1\
IDF_TOOLS_PATH:  C:\Users\username\.espressif

Goal: Add MQTT capability to a custom component in ESP-IDF

Issue:

../components/mqtt/include/mqtt.h:24:10: fatal error: mqtt_client.h: No such file or directory
 #include "mqtt_client.h"

Project Directory Contents:

enter image description here

Contents of /components/mqtt/CMakeLists.txt:

idf_component_register(SRCS "mqtt.c"
                        INCLUDE_DIRS "include"
                        REQUIRES mqtt esp-tls)

I added the mqtt and esp-tls required components to the CMakeLists.txt file as directed by the ESP-IDF documentation here: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/build-system.html#idf-component-commands

Attempt to Fix 1 My first attempt at fixing this code was to update the contents of /components/mqtt/CMakeLists.txt to directly reference the required include files:

idf_component_register(SRCS 
                        "mqtt.c"
                        "C:/Users/user/source/esp-idf-v4.3.1/components/mqtt/esp-mqtt/mqtt_client.c"
                        "C:/Users/user/source/esp-idf-v4.3.1/components/esp-mqtt/lib/mqtt_msg.c"
                        "C:/Users/user/source/esp-idf-v4.3.1/components/esp-mqtt/lib/mqtt_outbox.c"
                        "C:/Users/user/source/esp-idf-v4.3.1/components/esp-mqtt/lib/platform_esp32_idf.c"
                    INCLUDE_DIRS 
                        "include"
                        "C:/Users/user/source/esp-idf-v4.3.1/components/mqtt/esp-mqtt/include" 
                        "C:/Users/user/source/esp-idf-v4.3.1/components/esp-tls"
                        "C:/Users/user/source/esp-idf-v4.3.1/components/mbedtls/mbedtls/include"
                        "C:/Users/user/source/esp-idf-v4.3.1/components/mbedtls/port/include"
                        "C:/Users/user/source/esp-idf-v4.3.1/components/tcp_transport/include"
                        "C:/Users/user/source/esp-idf-v4.3.1/components/nghttp/port/include"
                    PRIV_INCLUDE_DIRS "C:/Users/user/source/esp-idf-v4.3.1/components/mqtt/esp-mqtt/lib/include" 
                    REQUIRES lwip nghttp mbedtls)

This builds, but there has to be a better way to build the mqtt component. Can anyone help me modify my CMakeLists.txt file to properly pull the esp-idf mqtt component in?


Solution

  • You have a naming conflict. You're trying to create a custom component named mqtt which requires the system component named mqtt and you expect the build system to figure it out. That's just asking for trouble :)

    Name your component myproject-mqtt or something. Watch out for similar problems with your components wifi and ble.