2

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

未定义对“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

提前感谢您的任何反馈。

4

1 回答 1

5

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

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

请参阅ESP IDF 文档中的详细信息

于 2021-04-11T15:00:06.683 回答