Search code examples
ccmakeesp-idf

ESP-IDF splitting project into multiple files


So I'm learning ESP-IDF, moving from Arduino and I was creating my first project and I tried following the docs on creating a component but I'm getting some errors when flashing regarding Pill.c (main c file) not finding the function defined in HomeAssistant.h (component created by me).

Full error:

C:\Espressif\tools\cmake\3.24.0\bin\cmake.exe --build C:\Users\amng8\Sync\PROJECTS\Pill\cmake-build-debug --target flash -j16
[1/7] Performing build step for 'bootloader'
[1/1] cmd.exe /C "cd /D C:\Users\amng8\Sync\PROJECTS\Pill\cmake-build-debug\bootloader\esp-idf\esptool_py && python C:/Espressif/frameworks/esp-idf-v5.0/components/partition_table/check_sizes.py --offset 0x8000 bootloader 0x1000 C:/Users/amng8/Sync/PROJECTS/Pill/cmake-build-debug/bootloader/bootloader.bin"
Bootloader binary size 0x6700 bytes. 0x900 bytes (8%) free.
[2/5] Linking CXX executable Pill.elf
FAILED: Pill.elf 
cmd.exe /C "cd . && C:\Espressif\tools\xtensa-esp32-elf\esp-2022r1-11.2.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-g++.exe -mlongcalls -Wno-frame-address  -g  @CMakeFiles\Pill.elf.rsp -o Pill.elf  && cd ."
c:/espressif/tools/xtensa-esp32-elf/esp-2022r1-11.2.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/11.2.0/../../../../xtensa-esp32-elf/bin/ld.exe: esp-idf/main/libmain.a(Pill.c.obj):(.literal.app_main+0x0): undefined reference to `init_mqtt'
c:/espressif/tools/xtensa-esp32-elf/esp-2022r1-11.2.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/11.2.0/../../../../xtensa-esp32-elf/bin/ld.exe: esp-idf/main/libmain.a(Pill.c.obj): in function `app_main':
C:/Users/amng8/Sync/PROJECTS/Pill/main/Pill.c:5: undefined reference to `init_mqtt'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

Pill.c

#include "HomeAssistant.h"

void app_main(void)
{
    init_mqtt();
}

Pill.c CMakeLists.txt

idf_component_register(SRCS "Pill.c" INCLUDE_DIRS "." REQUIRES HomeAssistant)

HomeAssistant.c (Not complete, but should be enough)

void init_mqtt() {
    esp_mqtt_client_config_t config = {
            .broker={
                    .address={
                            .uri="cool url"
                    }
            },
            .credentials={
                    .username="no username",
                    .authentication={
                            .password="want my password?"
                    }
            }
    };
    esp_mqtt_client_handle_t client = esp_mqtt_client_init(&config);
    esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
    esp_mqtt_client_start(client);
}

HomeAssistant.h

#include "esp_log.h"
#include "mqtt_client.h"

static void init_mqtt();

HomeAssistant Component CMakeLists.txt

idf_component_register(SRCS "HomeAssistant.c" INCLUDE_DIRS "include" REQUIRES mqtt)

File structure:

enter image description here

I'm using Clion as my IDE in case that help in any way.

My question is: Am I missing something or should I do this a different way?


Solution

  • You declared init_mqtt as static making it only visible to the file it is declared in