我正在尝试为ARM c编译裸机应用程序 Cortex-M3处理器和我在设置编译器/链接标志时遇到问题。我在Ubuntu Trusty上使用arm-none-eabi-gcc编译器4.9.3 20150529。
这是我的main.c:
#include <stdio.h>
void main() {
printf("Hello, world");
}
// Tons of clock initialization code ...
这是生成的链接命令:
arm-none-eabi-gcc -mthumb -mcpu=cortex-m3 -mlittle-endian -g -ggdb -T /home/user/hello-arm/cmake/../ldscripts/STM32F100XB_FLASH.ld -ffreestanding -nostdlib CMakeFiles/blinky.elf.dir/src/main.c.o CMakeFiles/blinky.elf.dir/src/stm32f1xx_hal_msp.c.o CMakeFiles/blinky.elf.dir/src/stm32f1xx_it.c.o CMakeFiles/blinky.elf.dir/src/stm32vl_discovery.c.o -o blinky.elf system/libhal_init.a system/libhal_cortex.a system/libhal_flash.a system/libhal_gpio.a system/libstartup.a system/libhal_rcc.a
这是错误:
/usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7-m/libc.a(lib_a-init.o): In function `__libc_init_array': init.c:(.text.__libc_init_array+0x20): undefined reference to `_init' /usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none- eabi/lib/armv7-m/libc.a(lib_a-sbrkr.o): In function `_sbrk_r': sbrkr.c (.text._sbrk_r+0xc): undefined reference to `_sbrk' /usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none eabi/lib/armv7-m/libc.a(lib_a-writer.o): In function `_write_r': writer.c:(.text._write_r+0x10): undefined reference to `_write' /usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none- eabi/lib/armv7-m/libc.a(lib_a-closer.o): In function `_close_r': closer.c:(.text._close_r+0xc): undefined reference to `_close' /usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none- eabi/lib/armv7-m/libc.a(lib_a-lseekr.o): In function `_lseek_r': lseekr.c:(.text._lseek_r+0x10): undefined reference to `_lseek' /usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none- eabi/lib/armv7-m/libc.a(lib_a-readr.o): In function `_read_r': readr.c: (.text._read_r+0x10): undefined reference to `_read' /usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none- eabi/lib/armv7-m/libc.a(lib_a-fstatr.o): In function `_fstat_r': fstatr.c:(.text._fstat_r+0xe): undefined reference to `_fstat' /usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none- eabi/lib/armv7-m/libc.a(lib_a-isattyr.o): In function `_isatty_r': isattyr.c:(.text._isatty_r+0xc): undefined reference to `_isatty'
我当时期待一个不同的错误:printf undefined。这告诉我正在找到stdio.h,并且正在链接stdlib。
最后值得注意的一件事。如果我删除-nostdlib,我会得到一个非常不同的错误:
/usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7- m/libg.a(lib_a-exit.o): In function `exit': exit.c:(.text.exit+0x16): undefined reference to `_exit' /usr/bin/../lib/gcc/arm-none- eabi/4.9.3/../../../../arm-none-eabi/lib/armv7-m/libg.a(lib_a-sbrkr.o): In function `_sbrk_r': sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk' /usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none- eabi/lib/armv7-m/libg.a(lib_a-writer.o): In function `_write_r': writer.c: (.text._write_r+0x10): undefined reference to `_write' /usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7- m/libg.a(lib_a-closer.o): In function `_close_r': closer.c: (.text._close_r+0xc): undefined reference to `_close' /usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7- m/libg.a(lib_a-lseekr.o): In function `_lseek_r': lseekr.c: (.text._lseek_r+0x10): undefined reference to `_lseek' /usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7- m/libg.a(lib_a-readr.o): In function `_read_r': readr.c: (.text._read_r+0x10): undefined reference to `_read' /usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7- m/libg.a(lib_a-fstatr.o): In function `_fstat_r': fstatr.c: (.text._fstat_r+0xe): undefined reference to `_fstat' /usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7- m/libg.a(lib_a-isattyr.o): In function `_isatty_r': isattyr.c: (.text._isatty_r+0xc): undefined reference to `_isatty' collect2: error: ld returned 1 exit status
[编辑]这是main.c的编译:
arm-none-eabi-gcc -DSTM32F100xB -mthumb -mcpu = cortex-m3 -mlittle-endian -nostdlib -ffreestanding -g -ggdb -I / home / user / hello-arm / apps / blinky / include -I / home / user / hello-arm / system / include / stm32f1xx_hal -I / home / user / hello-arm / system / include / cmsis -o CMakeFiles / blinky.elf.dir / src / main.co -c /home/user/hello-arm/apps/blinky/src/main.c
[EDIT2] main.c.o的对象转储:
00000000 <main>:
0: b580 push {r7, lr}
2: b082 sub sp, #8
4: af00 add r7, sp, #0
6: 4824 ldr r0, [pc, #144] ; (98 <main+0x98>)
8: f7ff fffe bl 0 <printf>
c: f7ff fffe bl 0 <HAL_Init>
10: f7ff fffe bl a8 <SystemClock_Config>
14: 4a21 ldr r2, [pc, #132] ; (9c <main+0x9c>)
...
看起来printf符号仍在那里。
[EDIT3]我只想明确注意到-nostdlib与没有它之间的区别是链接libc.a和libg.a
[EDIT4]链接器脚本在我下面粘贴的内容之外没有引用标准库:
/* Remove information from the standard libraries */
/DISCARD/ :
{
libc.a ( * )
libm.a ( * )
libgcc.a ( * )
}