ARM-M4工具链编译ELF时libdnk.a等库链接错误求助
Arm M4交叉编译链接错误求助
我使用v8.1.0版本的ARM-M4工具链编译ELF文件,C代码编译成功,但链接阶段出现以下错误:
[100%] Linking C executable ../test/bin/my_App.elf ld.lld: error: undefined symbol: time >>> referenced by clock.c >>> clock.c.o:(clock) in archive C:\compiler\toolchains\arm\v8.1.0\arm\lib/v7-m/thumb/vfpv4/hard/dp_d16/except\libdnk.a >>> referenced by tzset.c >>> tzset.c.o:(tzset) in archive C:\compiler\toolchains\arm\v8.1.0\arm\lib/v7-m/thumb/vfpv4/hard/dp_d16/except\libdnk.a >>> did you mean: ctime ld.lld: error: undefined symbol: __exidx_start >>> referenced by libunwind.cpp >>> libunwind.cpp.o:(__unw_init_local) in archive C:\compiler\toolchains\arm\v8.1.0\arm\lib/v7-m/thumb\libunwind.a >>> referenced by libunwind.cpp >>> libunwind.cpp.o:(libunwind::UnwindCursor<libunwind::LocalAddressSpace, libunwind::Registers_arm>::setInfoBasedOnIPRegister(bool)) in archive C:\compiler\toolchains\arm\v8.1.0\arm\lib/v7-m/thumb\libunwind.a ld.lld: error: undefined symbol: gettimeofday(timeval*, timezone*) >>> referenced by chrono.cpp >>> chrono.cpp.o:(std::__1::chrono::system_clock::now()) in archive C:\compiler\toolchains\arm\v8.1.0\arm\lib/v7-m/thumb/vfpv4/hard/dp_d16/except\libc++.a ld.lld: error: undefined symbol: getentropy >>> referenced by random.cpp >>> random.cpp.o:(std::__1::random_device::operator()()) in archive C:\compiler\toolchains\arm\v8.1.0\arm\lib/v7-m/thumb/vfpv4/hard/dp_d16/except\libc++.a clang: error: linker command failed with exit code 1 (use -v to see invocation)
我的编译环境是Arm M4架构,编译/链接配置如下:
编译标志
TOOLCHAIN ==>> "C:\compiler\toolchains\arm\v8.1.0\arm\include" set(COMMON_CFLAGS --target=armv7m-none-eabi -march=armv7-m -mcpu=cortex-m4 -mfpu=vfpv4-d16 -mlittle-endian -std=gnu99 -g3 -Wextra -Wall -I${TOOLCHAIN} -ffunction-sections -fdata-sections ) target_compile_options(${TEST_EXECUTABLE} PUBLIC ${COMMON_CFLAGS})
链接标志
set(LOCAL_LDFLAGS -T${MY_LINKER_FILE} -Wl,-Map,${TEST}/${TEST_EXECUTABLE}.map -Xlinker --allow-multiple-definition -Xlinker -mfpu=vfpv4-d16 -march=armv7-m -mfloat-abi=hard -mthumb ) target_link_options(${TEST_EXECUTABLE} PUBLIC ${LOCAL_LDFLAGS})
我是交叉编译新手,有几个疑问:
- 为什么编译纯C代码会需要链接
libc++.a? - 我尝试在链接标志中添加了以下库文件,但错误依旧:
TOOLCHAIN_LIB ==>> "C:\compiler\toolchains\arm\v8.1.0\arm\lib\v7-m\thumb" -mfloat-abi=hard -mthumb -I${TOOLCHAIN_LIB}/vfpv4/hard/sp_d16/libclang_rt.builtins.a -I${TOOLCHAIN_LIB}/vfpv4/hard/sp_d16/except/libdnk.a -I${TOOLCHAIN_LIB}/libunwind.a -I${TOOLCHAIN_LIB}/vfpv4/hard/sp_d16/except/libc++.a -I${TOOLCHAIN_LIB}/vfpv4/hard/sp_d16/except/libc++abi.a
请问有没有人遇到过类似问题?或者有可行的解决方法?
解决思路与方案
1. 为什么C代码会链接libc++.a?
你的代码或依赖的库(比如libdnk.a)可能间接引用了C标准库的功能,比如chrono或random_device,导致链接器自动尝试拉取libc++.a。另外,部分工具链的C库可能和C库存在依赖关联,即使只写C代码也可能触发这种情况。
2. 链接库的方式错误
你用-I指定库文件是完全错误的,-I是用来添加头文件搜索路径的,链接库需要用直接指定库文件完整路径或者-l(指定库名,需去掉lib前缀和.a后缀)。正确写法如下:
target_link_libraries(${TEST_EXECUTABLE} PUBLIC ${TOOLCHAIN_LIB}/vfpv4/hard/sp_d16/libclang_rt.builtins.a ${TOOLCHAIN_LIB}/vfpv4/hard/sp_d16/except/libdnk.a ${TOOLCHAIN_LIB}/libunwind.a ${TOOLCHAIN_LIB}/vfpv4/hard/sp_d16/except/libc++.a ${TOOLCHAIN_LIB}/vfpv4/hard/sp_d16/except/libc++abi.a )
3. 缺失符号的针对性处理
time/gettimeofday/getentropy:这些是POSIX标准的系统调用,Cortex-M4属于裸机环境,没有操作系统提供这些接口,需要自己实现桩函数(stub)。示例代码如下:#include <time.h> #include <sys/time.h> time_t time(time_t *t) { // 模拟时间值,实际项目可替换为硬件定时器读数 time_t mock_time = 1600000000; if (t) *t = mock_time; return mock_time; } int gettimeofday(struct timeval *tv, struct timezone *tz) { if (tv) { tv->tv_sec = 1600000000; tv->tv_usec = 0; } if (tz) { tz->tz_minuteswest = 0; tz->tz_dsttime = 0; } return 0; } int getentropy(void *buf, size_t buflen) { // 临时填充伪随机数,实际项目建议使用硬件RNG unsigned char *p = buf; for (size_t i=0; i<buflen; i++) { p[i] = i % 256; } return 0; }__exidx_start:这个符号是ARM EHABI异常处理所需,有两种处理方式:- 关闭异常处理:在
COMMON_CFLAGS中添加-fno-exceptions,同时可以移除libunwind.a和libc++abi.a的链接,减少不必要的依赖。 - 保留异常处理:修改链接脚本,添加以下段定义:
.ARM.exidx : { __exidx_start = .; *(.ARM.exidx* .gnu.linkonce.armexidx.*) __exidx_end = .; } > FLASH
- 关闭异常处理:在
4. 简化裸机链接配置
裸机环境下,建议明确指定需要的标准库,避免链接器自动引入不必要的依赖。比如添加-nostdlib后手动指定所需库,或者用-lc(C标准库)替代自动引入的C库(如果不需要C功能的话)。
内容的提问来源于stack exchange,提问作者user2986042
相关产品推荐
相关产品推荐

