ESP-IDF 项目结构问题:“未定义的函数引用”错误

问题描述 投票:0回答:1

我已经启动了一个基于 ESP-IDF 示例“blink”的项目,并尝试将其重新组织为更结构化的格式。我的目标是拥有这样的项目结构:

BLINK/
├── components/
│   ├── arduino/
│   ├── lib1/
│   │   ├── include/
│   │   │   └── foo.h
│   │   ├── src/
│   │   │   └── foo.c
│   │   └── CMakeLists.txt
│   └── ...
├── main/
│   └── main.c
├── CMakeLists.txt
├── sdkconfig
└── README.md

我将

main.c
文件夹中的
main
修改如下:

#include <stdio.h>
#include "sdkconfig.h"
#include "../components/lib1/include/foo.h"

void app_main() {
    printfoo(); 
    while (1) {      
    }
}

以下是我的

lib1
组件中的文件内容:

  • foo.h
    :

    #include <stdio.h>
    void printfoo();
    
  • foo.c
    :

    #include "foo.h"
    
    void printfoo() {
        printf("Hello from Foo\r\n");
    }
    
  • CMakeLists.txt
    BLINK/components/lib1/

    set(COMPONENT_SRCS "foo.c")
    set(COMPONENT_ADD_INCLUDEDIRS "." "include")
    register_component()
    

当我编译项目时,出现以下错误:

main.c:6: undefined reference to `printfoo'

我知道这个错误输出意味着链接器找不到位于 foo.c 中的函数 printfoo 的定义

对于可能导致此错误的原因以及如何解决它有什么建议吗?

我已按照 Component CMakeLists Files 上的 ESP32 指南进行操作,但仍然遇到此问题。

cmake esp32 arduino-esp32
1个回答
0
投票

我不知道您使用的是哪个版本的 ESP-IDF,但在 v5.x 版本中,常用的是

SRCS
INCLUDE_DIRS
,分别代替
COMPONENT_SRCS
COMPONENT_ADD_INCLUDEDIRS

在 ESP-IDF v5.x 中,以下 CMakeLists.txt 应该可以工作:

idf_component_register(SRCS "src/foo.c"
                       INCLUDE_DIRS "include")

然后,在 main 中,您应该能够通过以下方式包含

foo.h

#include "foo.h"
© www.soinside.com 2019 - 2024. All rights reserved.