Linker Error in Esp-Idf Framework (Undefined Reference)

Linker Error in Esp-Idf Framework (Undefined Reference)

I am learning to write apps on ESP32 platform. When I was trying to compile my code, I got this error from linker:

undefined reference to `Serial_Init'

Where Serial_Init is a function declared in a file serial_cli.h which is in the same directory and workspace. What's more, I declared some macros there, and can use them no problem in my code, so I really don't understand where the error comes from. Here's serial_cli.h:

#ifndef _SERIAL__H_
#define _SERIAL__H_

#include "driver/uart.h"

#define SERIAL_PORT         UART_NUM_1
#define RTS_SIG_PINOUT      UART_PIN_NO_CHANGE
#define CTS_SIG_PINOUT      UART_PIN_NO_CHANGE

/**
* @brief This function initialises serial communication with PC
* @param uart_config_t type pointer to structure containing needed parameters
* @param int size of RX and TX buffer (in bytes)
* @param QueueHandle_t type pointer to structure containing UART queue
*/
void Serial_Init(uart_config_t*, const int, QueueHandle_t*);


#endif /* _SERIAL_H_ */

And here's serial_cli.c:

#include "serial_cli.h"

void Serial_Init(uart_config_t* uart_config, const int buffer_size, QueueHandle_t* queue)
{
    ESP_ERROR_CHECK(uart_param_config(SERIAL_PORT, uart_config));
    ESP_ERROR_CHECK(uart_set_pin(SERIAL_PORT, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, RTS_SIG_PINOUT, CTS_SIG_PINOUT));
    ESP_ERROR_CHECK(uart_driver_install(SERIAL_PORT, buffer_size, buffer_size, 10, queue, 0));
}

And finally, main body of the app:

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "serial_cli.h"
#include "string.h"


void app_main(void)
{
    uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
        .rx_flow_ctrl_thresh = 122
    };
    QueueHandle_t queue;
    Serial_Init(&uart_config, 1024, &queue);
    char* test_str = "This is a test string.\n";
    while(1) {
        uart_write_bytes(SERIAL_PORT, (const char*)test_str, strlen(test_str));
        vTaskDelay(500);
    }
}

Below I also include complete output that I get from console:

D:\Tools\ESP_IDF\examples\get-started\blink>idf.py build
Executing action: all (aliases: build)
Running ninja in directory d:\tools\esp_idf\examples\get-started\blink\build
Executing "ninja all"...
[1/8] Performing build step for 'bootloader'
ninja: no work to do.
[5/6] Linking CXX executable blink.elf
FAILED: blink.elf
cmd.exe /C "cd . && D:\Tools\ESP_IDF_container\.espressif\tools\xtensa-esp32-elf\esp-2020r3-8.4.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-g++.exe  -mlongcalls -Wno-frame-address   @CMakeFiles\blink.elf.rsp  -o blink.elf 
 && cd ."
d:/tools/esp_idf_container/.espressif/tools/xtensa-esp32-elf/esp-2020r3-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: esp-idf/main/libmain.a(blink.c.obj):(.literal.app_main+0x8): undefined reference to `Serial_Init'
d:/tools/esp_idf_container/.espressif/tools/xtensa-esp32-elf/esp-2020r3-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: esp-idf/main/libmain.a(blink.c.obj): in function `app_main':
d:\tools\esp_idf\examples\get-started\blink\build/../main/blink.c:38: undefined reference to `Serial_Init'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
ninja failed with exit code 1

Thanks in advance for any feedback.

1 Answer

You need to add "serial_cli.c" to your main/CMakeLists.txt. Something like this:

idf_component_register(
    SRCS
        "blink.c"
        "serial_cli.c"
    ...

See details in ESP IDF documentation

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.