ESP32 IDF-V4.0 ninja编译无法引用外部静态库的问题

xsp1989
Posts: 3
Joined: Tue Mar 17, 2020 8:13 am

ESP32 IDF-V4.0 ninja编译无法引用外部静态库的问题

Postby xsp1989 » Tue Mar 17, 2020 8:49 am

我在自己的项目里面引用了三个外部静态库,并且修改了CMakeLists.txt文件,增加了库路径等,在最后链接的时候,(引入的库里面报错)提示一些函数未定义,而这些函数是在自己的源文件里面实现的,并且已经链接到了libmain.a里面。

开发环境:Vscode+ESP-IDF插件,(在cmd命令行里面调用idf.py build也是同样的错误)
另外:
在mingw32环境使用make方式可以正常编译通过,说明代码和库是没有问题的。

请问一下,有人是否也遇到了同样的问题。

以下为部分log:
d:/espressif/tools/xtensa-esp32-elf/esp-2019r2-8.2.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.2.0/../../../../xtensa-esp32-elf/bin/ld.exe: colink_server.c:(.text.serverTcpRead$constprop$2+0x20): undefined reference to `colinkTcpRead'
d:/espressif/tools/xtensa-esp32-elf/esp-2019r2-8.2.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.2.0/../../../../xtensa-esp32-elf/bin/ld.exe: ../main/Colink/lib/libcolink.a(colink_server.o): in function `colinkServerSubProcess':
colink_server.c:(.text.colinkServerSubProcess+0xc6): undefined reference to `colinkTcpSslConnect'
d:/espressif/tools/xtensa-esp32-elf/esp-2019r2-8.2.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.2.0/../../../../xtensa-esp32-elf/bin/ld.exe: colink_server.c:(.text.colinkServerSubProcess+0xd2): undefined reference to `colinkTcpConnect'
使用nm命令查看libmain.a库,里面包含了缺少的colinkTcpConnect函数
$ nm build/esp-idf/main/libmain.a | grep colinkTcpConnect
00000000 T colinkTcpConnect

下面是zigbeeGW.elf.rsp的文件内容
CMakeFiles/zigbeeGW.elf.dir/project_elf_src.c.obj esp-idf/esp_ringbuf/libesp_ringbuf.a esp-idf/driver/libdriver.a esp-idf/wpa_supplicant/libwpa_supplicant.a esp-idf/efuse/libefuse.a esp-idf/bootloader_support/libbootloader_support.a esp-idf/app_update/libapp_update.a esp-idf/spi_flash/libspi_flash.a esp-idf/nvs_flash/libnvs_flash.a esp-idf/esp_wifi/libesp_wifi.a esp-idf/esp_eth/libesp_eth.a esp-idf/lwip/liblwip.a esp-idf/tcpip_adapter/libtcpip_adapter.a esp-idf/esp_event/libesp_event.a esp-idf/pthread/libpthread.a esp-idf/espcoredump/libespcoredump.a esp-idf/esp32/libesp32.a esp-idf/xtensa/libxtensa.a esp-idf/esp_common/libesp_common.a esp-idf/esp_rom/libesp_rom.a esp-idf/soc/libsoc.a esp-idf/log/liblog.a esp-idf/heap/libheap.a esp-idf/freertos/libfreertos.a esp-idf/vfs/libvfs.a esp-idf/newlib/libnewlib.a esp-idf/cxx/libcxx.a esp-idf/app_trace/libapp_trace.a esp-idf/asio/libasio.a esp-idf/coap/libcoap.a esp-idf/console/libconsole.a esp-idf/nghttp/libnghttp.a esp-idf/esp-tls/libesp-tls.a esp-idf/esp_adc_cal/libesp_adc_cal.a esp-idf/esp_gdbstub/libesp_gdbstub.a esp-idf/tcp_transport/libtcp_transport.a esp-idf/esp_http_client/libesp_http_client.a esp-idf/esp_http_server/libesp_http_server.a esp-idf/esp_https_ota/libesp_https_ota.a esp-idf/protobuf-c/libprotobuf-c.a esp-idf/protocomm/libprotocomm.a esp-idf/mdns/libmdns.a esp-idf/esp_local_ctrl/libesp_local_ctrl.a esp-idf/esp_websocket_client/libesp_websocket_client.a esp-idf/expat/libexpat.a esp-idf/wear_levelling/libwear_levelling.a esp-idf/sdmmc/libsdmmc.a esp-idf/fatfs/libfatfs.a esp-idf/freemodbus/libfreemodbus.a esp-idf/jsmn/libjsmn.a esp-idf/json/libjson.a esp-idf/libsodium/liblibsodium.a esp-idf/mqtt/libmqtt.a esp-idf/openssl/libopenssl.a esp-idf/spiffs/libspiffs.a esp-idf/ulp/libulp.a esp-idf/unity/libunity.a esp-idf/wifi_provisioning/libwifi_provisioning.a esp-idf/main/libmain.a -Wl,--cref -Wl,--Map=E:/work/ESP32GW/1-src/ZigbeeGW/build/zigbeeGW.map -fno-rtti -fno-lto esp-idf/asio/libasio.a esp-idf/coap/libcoap.a esp-idf/esp_adc_cal/libesp_adc_cal.a esp-idf/esp_gdbstub/libesp_gdbstub.a esp-idf/esp_https_ota/libesp_https_ota.a esp-idf/esp_http_client/libesp_http_client.a esp-idf/esp_local_ctrl/libesp_local_ctrl.a esp-idf/esp_websocket_client/libesp_websocket_client.a esp-idf/expat/libexpat.a esp-idf/fatfs/libfatfs.a esp-idf/wear_levelling/libwear_levelling.a esp-idf/sdmmc/libsdmmc.a esp-idf/freemodbus/libfreemodbus.a esp-idf/jsmn/libjsmn.a esp-idf/libsodium/liblibsodium.a esp-idf/mqtt/libmqtt.a esp-idf/tcp_transport/libtcp_transport.a esp-idf/esp-tls/libesp-tls.a esp-idf/openssl/libopenssl.a esp-idf/spiffs/libspiffs.a esp-idf/ulp/libulp.a esp-idf/unity/libunity.a esp-idf/wifi_provisioning/libwifi_provisioning.a esp-idf/protocomm/libprotocomm.a esp-idf/esp_http_server/libesp_http_server.a esp-idf/nghttp/libnghttp.a esp-idf/protobuf-c/libprotobuf-c.a esp-idf/mdns/libmdns.a esp-idf/console/libconsole.a esp-idf/json/libjson.a esp-idf/esp_ringbuf/libesp_ringbuf.a esp-idf/driver/libdriver.a esp-idf/wpa_supplicant/libwpa_supplicant.a esp-idf/efuse/libefuse.a esp-idf/bootloader_support/libbootloader_support.a esp-idf/app_update/libapp_update.a esp-idf/spi_flash/libspi_flash.a esp-idf/nvs_flash/libnvs_flash.a esp-idf/esp_wifi/libesp_wifi.a esp-idf/esp_eth/libesp_eth.a esp-idf/lwip/liblwip.a esp-idf/tcpip_adapter/libtcpip_adapter.a esp-idf/esp_event/libesp_event.a esp-idf/pthread/libpthread.a esp-idf/espcoredump/libespcoredump.a esp-idf/esp32/libesp32.a esp-idf/xtensa/libxtensa.a esp-idf/esp_common/libesp_common.a esp-idf/esp_rom/libesp_rom.a esp-idf/soc/libsoc.a esp-idf/log/liblog.a esp-idf/heap/libheap.a esp-idf/freertos/libfreertos.a esp-idf/vfs/libvfs.a esp-idf/newlib/libnewlib.a esp-idf/cxx/libcxx.a esp-idf/app_trace/libapp_trace.a esp-idf/mbedtls/mbedtls/library/libmbedtls.a esp-idf/mbedtls/mbedtls/library/libmbedcrypto.a esp-idf/mbedtls/mbedtls/library/libmbedx509.a D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32/libcoexist.a D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32/libcore.a D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32/libespnow.a D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32/libmesh.a D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32/libnet80211.a D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32/libphy.a D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32/libpp.a D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32/librtc.a D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32/libsmartconfig.a esp-idf/esp_ringbuf/libesp_ringbuf.a esp-idf/driver/libdriver.a esp-idf/wpa_supplicant/libwpa_supplicant.a esp-idf/efuse/libefuse.a esp-idf/bootloader_support/libbootloader_support.a esp-idf/app_update/libapp_update.a esp-idf/spi_flash/libspi_flash.a esp-idf/nvs_flash/libnvs_flash.a esp-idf/esp_wifi/libesp_wifi.a esp-idf/esp_eth/libesp_eth.a esp-idf/lwip/liblwip.a esp-idf/tcpip_adapter/libtcpip_adapter.a esp-idf/esp_event/libesp_event.a esp-idf/pthread/libpthread.a esp-idf/espcoredump/libespcoredump.a esp-idf/esp32/libesp32.a esp-idf/xtensa/libxtensa.a esp-idf/esp_common/libesp_common.a esp-idf/esp_rom/libesp_rom.a esp-idf/soc/libsoc.a esp-idf/log/liblog.a esp-idf/heap/libheap.a esp-idf/freertos/libfreertos.a esp-idf/vfs/libvfs.a esp-idf/newlib/libnewlib.a esp-idf/cxx/libcxx.a esp-idf/app_trace/libapp_trace.a esp-idf/mbedtls/mbedtls/library/libmbedtls.a esp-idf/mbedtls/mbedtls/library/libmbedcrypto.a esp-idf/mbedtls/mbedtls/library/libmbedx509.a D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32/libcoexist.a D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32/libcore.a D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32/libespnow.a D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32/libmesh.a D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32/libnet80211.a D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32/libphy.a D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32/libpp.a D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32/librtc.a D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32/libsmartconfig.a -u esp_app_desc -L D:/espressif/esp-idf-v4.0/components/esp_wifi/lib_esp32 -u pthread_include_pthread_impl -u pthread_include_pthread_cond_impl -u pthread_include_pthread_local_storage_impl -L E:/work/ESP32GW/1-src/ZigbeeGW/build/esp-idf/esp32 -T esp32_out.ld -u app_main -L E:/work/ESP32GW/1-src/ZigbeeGW/build/esp-idf/esp32/ld -T esp32.project.ld -L D:/espressif/esp-idf-v4.0/components/esp32/ld -T esp32.peripherals.ld -u call_user_start_cpu0 -u ld_include_panic_highint_hdl D:/espressif/esp-idf-v4.0/components/xtensa/esp32/libhal.a -Wl,--gc-sections -L D:/espressif/esp-idf-v4.0/components/esp_rom/esp32/ld -T esp32.rom.ld -T esp32.rom.libgcc.ld -T esp32.rom.syscalls.ld -T esp32.rom.newlib-data.ld -T esp32.rom.newlib-funcs.ld -Wl,--undefined=uxTopUsedPriority -u vfs_include_syscalls_impl esp-idf/newlib/libnewlib.a -u newlib_include_locks_impl -u newlib_include_heap_impl -u newlib_include_syscalls_impl -u newlib_include_pthread_impl -u __cxa_guard_dummy -lstdc++ esp-idf/pthread/libpthread.a -u __cxx_fatal_exception -lgcov -lc -lm -lgcc ../main/Colink/lib/libcolink.a ../main/Colink/lib/libcolinkcJSON.a ../main/Colink/lib/libcolinkgw.a

xsp1989
Posts: 3
Joined: Tue Mar 17, 2020 8:13 am

Re: ESP32 IDF-V4.0 ninja编译无法引用外部静态库的问题

Postby xsp1989 » Wed Mar 18, 2020 3:58 am

这个问题看起来是静态库的依赖顺序问题,为啥传统的GNU Make不会有这个问题,而使用Ninja就会出现这个问题呢?

ESP_Gargamel
Posts: 61
Joined: Wed Nov 14, 2018 8:45 am

Re: ESP32 IDF-V4.0 ninja编译无法引用外部静态库的问题

Postby ESP_Gargamel » Thu Mar 19, 2020 5:33 am

不知道你工程结构是怎样的,你看下是否和你 component register 有关系,https://docs.espressif.com/projects/esp ... cmakelists,确认是否需要 REQUIRES 一些依赖。

xsp1989
Posts: 3
Joined: Tue Mar 17, 2020 8:13 am

Re: ESP32 IDF-V4.0 ninja编译无法引用外部静态库的问题

Postby xsp1989 » Sat Mar 21, 2020 3:55 am

谢谢您的解答!

问题已经解决,确实是库的顺序引起的依赖问题。
我这边的情况如下:
main组件里面的函数fun_main_a()调用了已经编译好的静态库中的函数fun_lib_1(),静态库中的函数fun_lib_1再调用main组件中的另外一个函数fun_main_b()
在编译的时候把fun_main_a()与fun_main_b()打包到了libmain.a静态库中,然后在链接阶段会出现找不到fun_main_a()或者fun_main_b()函数的情况。至于是找不到哪个函数取决于包含fun_lib_1()的库相对于libmain.a的位置。
我这边的解决方法是把fun_main_a()和fun_main_b()这两个函数分开,注册成另外一个组件,用REQUIRES声明来解决顺序问题。

但是还有个疑问就是用传统的GNU的方式为什么没有这个问题呢(使用make命令进行编译)?

ESP_Gargamel
Posts: 61
Joined: Wed Nov 14, 2018 8:45 am

Re: ESP32 IDF-V4.0 ninja编译无法引用外部静态库的问题

Postby ESP_Gargamel » Fri Mar 27, 2020 2:13 am

好的,方便的话,也提供一个 sample project,这样一方面我们可以再分析一下,另一方面也可以给其他开发者参考。

Who is online

Users browsing this forum: No registered users and 23 guests