Gcc Stdio/Stdlib在向链接器传递-ffreestanding和-nostdlib标志后仍在链接

Gcc Stdio/Stdlib在向链接器传递-ffreestanding和-nostdlib标志后仍在链接,gcc,arm,embedded,bare-metal,Gcc,Arm,Embedded,Bare Metal,我正在尝试为ARMC编译一个裸机应用程序 Cortex-M3处理器和我在设置编译器/链接标志时遇到问题。我在Ubuntu Trusty上,使用arm none eabi gcc编译器4.9.3 20150529 这是我的主要观点 #include <stdio.h> void main() { printf("Hello, world"); } // Tons of clock initialization code ... 下面是错误: 但我预期会出现另一个错误:prin

我正在尝试为ARMC编译一个裸机应用程序 Cortex-M3处理器和我在设置编译器/链接标志时遇到问题。我在Ubuntu Trusty上,使用arm none eabi gcc编译器4.9.3 20150529

这是我的主要观点

#include <stdio.h>
void main() {
    printf("Hello, world");
}
// Tons of clock initialization code ...
下面是错误:

但我预期会出现另一个错误:printfundefined。这告诉我正在找到stdio.h,并且正在链接stdlib

最后值得一提的是。如果我删除-nostlib,我会得到一个稍微不同的错误:

[编辑]下面是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/stm32f1x_hal-I/home/user/hello arm/system/include/cmsis-o CMakeFiles/blinky.elf.dir/src/main.c-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>)
        ...

您向我们展示了链接器命令是什么,但是编译(而不是链接)时命令行选项是什么样子的。在编译时,您需要使用
-nostinc
-ffreestanding
来禁止包含标准库标题!谢谢你的回复。实际上,在编译过程中,我确实包含了--ffrerestanding,但在我看来,这不应该是必需的。东西应该编译,但我期望的错误是引用未定义,因为我试图阻止stdlib被链接,不管main是否认为它们可以通过header使用。因此,我没有包含编译命令:)。请检查主对象文件,确保常量字符串printf没有被优化器更改为其他内容。我使用的是旧的工具链。我会尝试更新它。(编辑)运气不好。不要忘记检查链接器脚本
STM32F100XB\u FLASH.ld
文件。您也可以通过链接器脚本拉入C lib。您向我们展示了链接器命令是什么,但是编译(而不是链接)时命令行选项是什么样子的。在编译时,您需要使用
-nostinc
-ffreestanding
来禁止包含标准库标题!谢谢你的回复。实际上,在编译过程中,我确实包含了--ffrerestanding,但在我看来,这不应该是必需的。东西应该编译,但我期望的错误是引用未定义,因为我试图阻止stdlib被链接,不管main是否认为它们可以通过header使用。因此,我没有包含编译命令:)。请检查主对象文件,确保常量字符串printf没有被优化器更改为其他内容。我使用的是旧的工具链。我会尝试更新它。(编辑)运气不好。不要忘记检查链接器脚本
STM32F100XB\u FLASH.ld
文件。您也可以通过链接器脚本拉入C库。
/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'
/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
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>)
        ...
  /* Remove information from the standard libraries */
  /DISCARD/ :
  {
    libc.a ( * )
    libm.a ( * )
    libgcc.a ( * )
  }