在clion上运行 stm32F407标准库函数代码时出现的问题
"C:\clion\CLion 2023.1.4\bin\cmake\win\x64\bin\cmake.exe" --build D:\Allfile\stm32f4\untitled4\cmake-build-debug --target untitled4.elf -j 14
[1/1] Linking C executable untitled4.elf
FAILED: untitled4.elf
cmd.exe /C "cd . && D:\leaningsoftware\gcc-arm-none-eabi\bin\arm-none-eabi-gcc.exe -g -Wl,-gc-sections,--print-memory-usage,-Map=D:/Allfile/stm32f4/untitled4/cmake-build-debug/untitled4.map -mcpu=cortex-m4 -mthumb -mthumb-interwork -T D:/Allfile/stm32f4/untitled4/STM32F407ZGTX_FLASH.ld CMakeFiles/untitled4.elf.dir/FWLIB/Src/misc.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_adc.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_can.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_crc.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_cryp.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_cryp_aes.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_cryp_des.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_cryp_tdes.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_dac.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_dbgmcu.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_dcmi.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_dma.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_dma2d.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_exti.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_flash.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_flash_ramfunc.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_fsmc.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_gpio.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_hash.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_hash_md5.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_hash_sha1.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_i2c.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_iwdg.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_ltdc.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_pwr.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_rcc.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_rng.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_rtc.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_sai.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_sdio.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_spi.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_syscfg.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_tim.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_usart.c.obj CMakeFiles/untitled4.elf.dir/FWLIB/Src/stm32f4xx_wwdg.c.obj CMakeFiles/untitled4.elf.dir/SYSTEM/Src/delay.c.obj CMakeFiles/untitled4.elf.dir/SYSTEM/Src/sys.c.obj CMakeFiles/untitled4.elf.dir/SYSTEM/Src/usart.c.obj CMakeFiles/untitled4.elf.dir/Startup/startup_stm32f407zgtx.s.obj CMakeFiles/untitled4.elf.dir/USER/Src/main.c.obj CMakeFiles/untitled4.elf.dir/USER/Src/stm32f4xx_it.c.obj CMakeFiles/untitled4.elf.dir/USER/Src/system_stm32f4xx.c.obj -o untitled4.elf && cmd.exe /C "cd /D D:\Allfile\stm32f4\untitled4\cmake-build-debug && arm-none-eabi-objcopy -Oihex D:/Allfile/stm32f4/untitled4/cmake-build-debug/untitled4.elf D:/Allfile/stm32f4/untitled4/cmake-build-debug/untitled4.hex && arm-none-eabi-objcopy -Obinary D:/Allfile/stm32f4/untitled4/cmake-build-debug/untitled4.elf D:/Allfile/stm32f4/untitled4/cmake-build-debug/untitled4.bin""
d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-sbrkr.o): in function _sbrk_r': sbrkr.c:(.text._sbrk_r+0xc): undefined reference to
_sbrk'
d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-writer.o): in function _write_r': writer.c:(.text._write_r+0x14): undefined reference to
_write'
d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-closer.o): in function _close_r': closer.c:(.text._close_r+0xc): undefined reference to
_close'
d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-fstatr.o): in function _fstat_r': fstatr.c:(.text._fstat_r+0x12): undefined reference to
_fstat'
d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-isattyr.o): in function _isatty_r': isattyr.c:(.text._isatty_r+0xc): undefined reference to
_isatty'
d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-lseekr.o): in function _lseek_r': lseekr.c:(.text._lseek_r+0x14): undefined reference to
_lseek'
d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-readr.o): in function _read_r': readr.c:(.text._read_r+0x14): undefined reference to
_read'
d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-abort.o): in function abort': abort.c:(.text.abort+0xa): undefined reference to
_exit'
d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-signalr.o): in function _kill_r': signalr.c:(.text._kill_r+0x12): undefined reference to
_kill'
d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: d:/leaningsoftware/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-signalr.o): in function _getpid_r': signalr.c:(.text._getpid_r+0x0): undefined reference to
_getpid'
Memory region Used Size Region Size %age Used
CCMRAM: 0 GB 64 KB 0.00%
RAM: 4344 B 128 KB 3.31%
FLASH: 34060 B 1 MB 3.25%
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
我参考了GPT:
给你提出以下建议:
1:这个问题是关于在编译STM32项目时出现undefined reference to _sbrk的错误,回答者给出了一些解决方案,如使用gcc命令来调用链接器,或者在命令行中重复libc.a和libgcc.a两次,或者使用-specs=nano.specs -specs=nosys.specs选项。
2:这个问题是关于在使用PolarSSL库创建HTTPS服务器时出现undefined reference to _kill, _getpid and _sbrk的错误,回答者指出这是因为PolarSSL库中使用了malloc()和free()函数,需要提供_sbrk, _kill和_getpid的实现。
3:这篇博客介绍了在STM32CubeIDE中编译STM32项目时出现undefined reference to的错误的原因和解决方法,如添加相应的库文件,或者使用-specs=nano.specs -specs=nosys.specs选项。
你可以点击下面的链接查看更多的信息:
这里有和你类似的问题,可以参考看一下https://bbs.csdn.net/topics/396840868。
根据提供的错误消息,问题出在链接阶段。错误信息显示了一系列与标准库函数相关的未定义引用。这通常发生在链接器无法找到标准库函数的实现时。
这个问题可能是由于编译器无法正确找到所需的标准库路径而导致的。你可以尝试以下几种方法来解决这个问题:
1,确保你已经正确配置了编译器和标准库路径。在CLion中,你可以在项目的CMakeLists.txt文件中添加以下内容来指定编译器和标准库路径:
set(CMAKE_C_COMPILER "/path/to/arm-none-eabi-gcc")
set(CMAKE_CXX_COMPILER "/path/to/arm-none-eabi-g++")
set(CMAKE_C_FLAGS "-mcpu=cortex-m4 -mthumb")
set(CMAKE_CXX_FLAGS "-mcpu=cortex-m4 -mthumb")
include_directories("/path/to/stm32f4xx_stdperiph_lib")
link_directories("/path/to/stm32f4xx_stdperiph_lib")
请确保将上述路径替换为实际的路径。
2,检查是否正确包含了所需的标准库头文件。在你的源代码文件中,确保包含了正确的标准库头文件,例如:
#include <stdio.h>
#include <stdlib.h>
// 其他标准库头文件
3,检查是否包含了正确的链接器脚本文件。在你的CMakeLists.txt文件中,确保通过-T选项指定了正确的链接器脚本文件,例如:
set(LINKER_SCRIPT "/path/to/STM32F407ZGTX_FLASH.ld")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -T ${LINKER_SCRIPT}")
请将上述路径替换为实际的路径。
如果你已经尝试了上述方法仍然无法解决问题,还有一些可能的原因需要进一步排查。这包括编译器版本兼容性问题、缺失的标准库文件等。你可以参考官方文档、论坛或社区来寻求更多关于在CLion上使用STM32F407标准库的支持和解决方案。
一些标准C库函数没有被实现,如_sbrk、_write、_close,你导入库,没导入正确吧,或者缺失了什么
参考官方 https://www.st.com/en/microcontrollers-microprocessors/stm32f4-series/documentation.html