c - ESP-IDF 框架中的链接器错误(未定义引用)

标签 c esp32 esp-idf

我正在学习在 ESP32 平台上编写应用程序。当我试图编译我的代码时,我从链接器收到了这个错误:

undefined reference to `Serial_Init'

其中 Serial_Init 是在同一目录和工作区中的文件 serial_cli.h 中声明的函数。更重要的是,我在那里声明了一些宏,并且可以在我的代码中使用它们没有问题,所以我真的不明白错误来自哪里。 这是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_ */

这是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));
}

最后是应用程序的主体:

#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);
    }
}

下面我还包含了从控制台获得的完整输出:

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

提前感谢您的任何反馈。

最佳答案

您需要将“serial_cli.c”添加到您的main/CMakeLists.txt。像这样:

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

详见 ESP IDF documentation

关于c - ESP-IDF 框架中的链接器错误(未定义引用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67039814/

相关文章:

c - Eclipse CDT 中的 C 相当于 "Open Implementation"

c++ - 使用 esp32 的 AWS iot 接收来自某些主题的消息,但不接收其他主题的消息?

c - ESP32s NodeMCU 无法识别 SD 卡突破 : "Card Mount Failed"

timer - 如何按需删除和重启 esp32 arduino(步进电机 Controller 应用程序)的硬件定时器(用于中断)

esp32 - 为 ESP32 实现 Wi-Fi Direct

protocol-buffers - 将 Google Protobuffers(C++) 与 ESP-IDF 结合使用

c - 我如何在父子进程之间共享内存,以便在它们死亡时自动释放内存

c - 段错误/Dev C 崩溃

c - SHA1 以流方式对长度前缀的消息进行校验和

c - 如何在 esp-idf 中从 NTP 服务器重新同步时间?