【问题标题】:Create esp32 static library based on multiple components创建基于多个组件的 esp32 静态库
【发布时间】:2021-09-23 16:12:31
【问题描述】:

我是 esp-idf 开发的新手,我想为 esp32 构建一个静态库。 我已阅读 espressif 文档(可在此处获得:Programming Guide),但无法创建正确的 .a 文件。

我们的想法是开发一个新的包装器和函数库,其中包括来自esp-dsp 库的函数。我已将 esp-dsp 组件添加到我的项目中并创建了我自己的组件:my_component

现在有我的项目结构:

- first_project/
             - CMakeLists.txt
             - sdkconfig
             - components/ - esp-dsp/ - CMakeLists.txt
                                      - ...
                                      - ...
                           - my_component/ - CMakeLists.txt
                                           - my_component.c
                                           - include/ 
                                                     - my_component.h
             - main/       - CMakeLists.txt
                           - main.c

             - build/
    

以下是CMakeLists.txt 文件:

CMakeLists.txt (first_project)

cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(first_test)

CMakeLists.txt(主)

idf_component_register(SRCS "main.c"
                    INCLUDE_DIRS "."
                    REQUIRES my_component esp-dsp)

CMakeLists.txt (my_component)

idf_component_register(SRCS "my_component.c"
                    INCLUDE_DIRS "include"
                    REQUIRES esp-dsp)

my_component.c中有来自esp-dsp的函数

使用之前的文件,我设法创建了一个二进制文件以闪存到 ESP32 板上,但下一步是构建一个静态库 (my_library.a),其中包含 my_component.c 中的函数。

所以我尝试修改CmakeLists.txt (main)以创建关于组件my_component的静态库[MyStaticLib.a]

cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(first_test)

ADD_LIBRARY( MyStaticLib STATIC
             components/my_component/my_component.c )
SET( APP_EXE StaticTest )
ADD_EXECUTABLE( ${APP_EXE}
                main/main.c ) 

TARGET_LINK_LIBRARIES( ${APP_EXE}
                    MyStaticLib )

但是,在构建过程中,我看不到my_componentinclude 路径文件夹。然后编译失败是因为my_component.h:No such file or directory

FAILED: CMakeFiles/MyStaticLib.dir/components/my_component/my_component.c.obj 
ccache C:\Users\julien\.espressif\tools\xtensa-esp32-elf\esp-2020r3-8.4.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-gcc.exe   -mlongcalls -Wno-frame-address -g -MD -MT CMakeFiles/MyStaticLib.dir/components/my_component/my_component.c.obj -MF CMakeFiles\MyStaticLib.dir\components\my_component\my_component.c.obj.d -o CMakeFiles/MyStaticLib.dir/components/my_component/my_component.c.obj   -c ../components/my_component/my_component.c
../components/my_component/my_component.c:2:10: fatal error: my_component.h: No such file or directory

仅当我从 main 修改 CMakeLists.txt 以构建静态库时才会出现问题。

我虽然 CMakeLists.txt 文件中的变量 INCLUDE_DIRS 可以解决问题,但徒劳无功。

知道我在 CMakeLists.txt 参数中做错了什么以便构建 .a 静态库以用作其他项目中的组件吗?

问候。

【问题讨论】:

    标签: components static-libraries esp32 esp-idf


    【解决方案1】:

    只需像您一样添加您的组件来构建示例。 您将在build/esp-idf/ 文件夹下看到所有构建为库(.a) 的组件。

    在其他项目中使用 libmy_component.a 以及 libesp_dsp.a

    【讨论】:

    • 构建按预期工作,我可以生成libmy_component.a。但是,当我想将其添加到新项目时,编译无法按预期工作。我将esp-dsp 添加为组件,并使用命令(add_libraryset propertytarget_link_libraries)添加了我的静态库。来自esp-dsp的引用有错误:../main/../lib/my_libray/libmy_component.a(my_component.c.obj):(.literal.process_and_show2+0x0): undefined reference to dsps_fft_w_table_fc32
    • 我将libmy_component.alibesp_dsp.a 都链接到我的项目,并且现在运行良好。我虽然可以只添加 libmycomponent.a 作为静态库和 esp-dsp 作为组件,但它没有按预期工作。
    猜你喜欢
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多