相關問題:
一、undefined reference to `_exit
undefined reference to ‘end‘
warning: _close is not implemented and will always fail
一、環境:
ubuntu24.04實體機、
arm-none-eabi-gcc? ? ? gcc version 13.2.1 20231009 (15:13.2.rel1-2)
二、問題:
/usr/lib/gcc/arm-none-eabi/13.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/13.2.1/../../../arm-none-eabi/lib/libnosys.a(sbrk.o): in function `_sbrk':
/build/newlib-38V0JC/newlib-4.4.0.20231231/build/arm-none-eabi/libgloss/../../../libgloss/libnosys/sbrk.c:21:(.text+0x28): undefined reference to `end'
三、分析:
這個錯誤來自 newlib-nano 的 libnosys/sbrk.c
,它試圖使用符號 end
來初始化堆的起始地址,但你的鏈接器腳本里沒有定義它。
符號名必須是 _end
(帶下劃線),因為 sbrk.c
中用的是 extern char end;
,而 GNU LD 默認會把 _end
和 end
視為不同符號。
四、解決:
SECTIONS{
? ? . = 0;
? ? .text : {
? ? ? ? user/startup_hc32l13x.o
? ? ? ? *(.text)
? ? }
? ? .data : {
? ? ? ? *(.data)
? ? }
? ? .bss : {
? ? ? ? *(.bss)
? ? }
}
SECTIONS{
? ? . = 0;
? ? .text : { ? ? ? /*寫成 .text:{ 不對,要有空格。*/
? ? ? ? *(.text) ? ?/*寫成 *{.text} 不對,要為小括號。*/
? ? }
? ? .data : {
? ? ? ? *(.data)
? ? }
? ? .bss : {
? ? ? ? _sbss = .; ? ? ? ? /* define a global symbol at bss start */
? ? ? ? __bss_start__ = _sbss;
? ? ? ? *(.bss)
? ? ? ? *(.bss*)
? ? ? ? *(COMMON)
? ? ? ? . = ALIGN(4);
? ? ? ? _ebss = .; ? ? ? ? /* define a global symbol at bss end */
? ? ? ? __bss_end__ = _ebss;
? ? ? ? end ? ?= .;
? ? }
}
添加?end ? ?= .; 就好了