You need to enable JavaScript to run this app. arm-none-eabi-gcc编译树莓派2/BBB裸机FreeRTOS时调用sqrt报错
优惠活动
大模型
产品
解决方案
定价
更多

arm-none-eabi-gcc编译树莓派2/BBB裸机FreeRTOS时调用sqrt报错

开发背景
  • 具备Cortex-M MCU开发经验,使用过Keil、Nordic SDK完成BLE相关代码编译,有Cortex-M平台FreeRTOS开发经历
  • 当前开展Cortex-A架构裸机程序开发测试,参考社区公开的BeagleBone Black(简称BBB)FreeRTOS移植仓库开展学习,树莓派2平台的同类问题与BBB场景表现一致
  • 开发环境为Linux系统,安装gcc-arm-none-eabi 9.2.1工具链,使用VS Code集成终端执行make构建流程,仓库原有配置可正常编译生成固件,烧录至BBB后可正常运行
编译问题排查与解决过程

解决rand()函数链接报错

  • 初始扩展功能需要调用stdlib.h中的rand()函数,首次编译报错undefined reference to rand
  • 排查仓库构建脚本发现:makefile的可执行文件生成环节,依赖makedefs_ti中定义的LIB_GCC、LIB_C两个工具链路径变量;原仓库默认适配arm-none-eabi 4.7.3版本的路径,与本机实际安装的9.2.1版本不匹配
  • 最初直接在makefile链接环节添加-lc、-lg参数时,报arm-none-eabi-ld: cannot find -lc,后续手动修正makedefs_ti中的路径配置:
# 修正后的工具库路径配置
LIB_GCC= /usr/lib/gcc/arm-none-eabi/9.2.1
LIB_C= /usr/lib/arm-none-eabi/lib
  • 对应修改makefile链接环节参数,最终链接命令为:
$(LD) -o $@.out $< -T bbb.ld -Map bbb.map $(APP_LIB) $(LDFLAGS) $(RUNTIMELIB) -L $(LPATH) \
   -L $(LIB_GCC) -lgcc -L $(LIB_C) -lc -lg
  • 调整后,main.c中调用rand()可正常编译,后续在新增的独立.c文件中调用rand()也无报错。

新增代码调用sqrt()触发链接异常

  • 在rtos目录下新增独立的.c/.h文件,复用目录现有makefile编译规则,封装的函数内部调用math.h中的sqrt()接口
  • 在main函数中调用该封装函数时,所有相关源文件都正确引入了math.h,编译仍中断报undefined reference to sqrt
  • 按照常规交叉编译经验,在链接命令末尾追加-lm参数,调整后触发新的链接错误:
arm-none-eabi-ld: /usr/lib/arm-none-eabi/lib/libm.a(lib_a-w_sqrt.o): in function `sqrt':
/build/newlib-CVVEyx/newlib-3.3.0/build/arm-none-eabi/newlib/libm/math/../../../../../newlib/libm/math/w_sqrt.c:62: undefined reference to `__aeabi_dcmpun'
arm-none-eabi-ld: /build/newlib-CVVEyx/newlib-3.3.0/build/arm-none-eabi/newlib/libm/math/../../../../../newlib/libm/math/w_sqrt.c:63: undefined reference to `__aeabi_dcmplt'
arm-none-eabi-ld: /build/newlib-CVVEyx/newlib-3.3.0/build/arm-none-eabi/newlib/libm/math/../../../../../newlib/libm/math/w_sqrt.c:64: undefined reference to `__errno'
arm-none-eabi-ld: /build/newlib-CVVEyx/newlib-3.3.0/build/arm-none-eabi/newlib/libm/math/../../../../../newlib/libm/math/w_sqrt.c:65: undefined reference to `__aeabi_ddiv'
arm-none-eabi-ld: /usr/lib/arm-none-eabi/lib/libm.a(lib_a-e_sqrt.o): in function `__ieee754_sqrt':
/build/newlib-CVVEyx/newlib-3.3.0/build/arm-none-eabi/newlib/libm/machine/arm/../../../../../../newlib/libm/machine/arm/../../math/e_sqrt.c:110: undefined reference to `__aeabi_dmul'
arm-none-eabi-ld: /build/newlib-CVVEyx/newlib-3.3.0/build/arm-none-eabi/newlib/libm/machine/arm/../../../../../../newlib/libm/machine/arm/../../math/e_sqrt.c:110: undefined reference to `__aeabi_dadd'
arm-none-eabi-ld: /build/newlib-CVVEyx/newlib-3.3.0/build/arm-none-eabi/newlib/libm/machine/arm/../../../../../../newlib/libm/machine/arm/../../math/e_sqrt.c:117: undefined reference to `__aeabi_dsub'
arm-none-eabi-ld: /build/newlib-CVVEyx/newlib-3.3.0/build/arm-none-eabi/newlib/libm/machine/arm/../../../../../../newlib/libm/machine/arm/../../math/e_sqrt.c:117: undefined reference to `__aeabi_ddiv'
make: *** [makefile:26: app] Errore 1
关键复现特征
  • 直接在main.c中调用sqrt()可正常编译,仅在新增的独立.c文件中调用sqrt()才会触发上述报错
  • 同一份代码导入Code Composer Studio工程编译时,在新增.c文件中调用sqrt、rand均可正常运行,当前make构建流程与IDE默认配置的差异无法定位,需要协助排查解决

内容的提问来源于stack exchange,提问作者Salvatore Torsello

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.14 07:45:07