使用 arm-none-eabi-gcc、newlib 和 cmake 连接 C/C++ STM32 项目

Linking C/C++ STM32 project using arm-none-eabi-gcc, newlib and cmake

本文关键字:C++ STM32 项目 连接 cmake arm-none-eabi-gcc newlib 使用      更新时间:2023-10-16

我在使用 CMake 链接我的 STM32 项目时遇到困难。生成的链接命令为:

/Users/jeremy/gcc-arm-none-eabi-6-2017-q2-update/bin/arm-none-eabi-g++  -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fdata-sections -ffunction-sections -g -Og -gdwarf-2 -MMD -MP  -std=c++11 -Wl,-search_paths_first -Wl,-headerpad_max_install_names -specs=nano.specs -T/Users/jeremy/stm32l432kc_freertos_template/STM32L432KCUx_FLASH.ld -Wl,-Map=target.map,--cref -Wl,--gc-sections 
< ... lots of .o files here ...>
-o stm32l432kc_freertos -lc -lm -lnosys

不幸的是,我遇到了两组错误。首先是:

arm-none-eabi/bin/ld: warning: cannot find entry symbol arch_paths_first; defaulting to 0000000008000190

这表示没有条目符号,但在 LD 文件中的第一行代码是:ENTRY(Reset_Handler).符号Reset_Handler在链接文件startup_stm32l432xx.s中定义。

第二组错误与 stdlib 有关:

/Users/jeremy/gcc-arm-none-eabi-6-2017-q2-update/bin/../lib/gcc/arm-none-eabi/6.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/fpv4-sp/hard/libg_nano.a(lib_a-signalr.o): In function `_kill_r':
signalr.c:(.text._kill_r+0xe): undefined reference to `_kill'
/Users/jeremy/gcc-arm-none-eabi-6-2017-q2-update/bin/../lib/gcc/arm-none-eabi/6.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/fpv4-sp/hard/libg_nano.a(lib_a-signalr.o): In function `_getpid_r':
signalr.c:(.text._getpid_r+0x0): undefined reference to `_getpid'

这应该通过链接-lnosys来解决,但链接器似乎忽略了这一点。

本质上,链接器似乎忽略了 LD 文件中的某些指令,并忽略了我传递的一些标志。我意识到这可能是我做错了什么,但我看不出它是什么。

如果我添加-specs=nosys.specs后两个错误就会消失,但这不应该是必需的吗?有人可以帮我了解这里出了什么问题吗?

看起来 OSX 上的 CMake 正在添加 gcc arm 工具链不支持的-Wl,-search_paths_first。解决方法是将其添加到 CMakelists.txt 文件中:

if ( APPLE )
string ( REPLACE "-Wl,-search_paths_first" "" CMAKE_C_LINK_FLAGS ${CMAKE_C_LINK_FLAGS} )
string ( REPLACE "-Wl,-search_paths_first" "" CMAKE_CXX_LINK_FLAGS ${CMAKE_CXX_LINK_FLAGS} )
endif ()

修复从这里获取:https://github.com/kiibohd/controller/blob/master/Lib/CMake/build.cmake

仍然不知道-lnosys被忽略了。