gcc - 为什么在__libc_init_array中有对_init的 undefined reference ?

标签 gcc linker yagarto

我正在尝试使用Yagarto和Eclipse为ARM微 Controller 平台构建一个简单的项目。在我的启动代码中,我有以下代码(我认为这是相当标准且无趣的):

void Reset_Handler(void)
{
  /* Initialize data and bss */
  __Init_Data();

  /* Call CTORS of static objects */
  __libc_init_array();

  /* Call the application's entry point.*/
  main();

  while(1) { ; }
}

除非我注释掉对__libc_init_array()的调用,否则从链接器中会收到以下错误:
arm-none-eabi-g++ -nostartfiles -mthumb -mcpu=cortex-m4 -TC:/Users/mark/workspace/stm32_cpp_test/STM32F40x_1024k_192k_flash.ld -gc-sections -Wl,-Map=test_rom.map,--cref,--no-warn-mismatch -o stm32_cpp_test "system\\syscalls.o" "system\\startup_stm32f4xx.o" "system\\mini_cpp.o" "system\\cmsis\\system_stm32f4xx.o" main.o 
d:/utils/yagarto/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/lib/thumb/v7m\libg.a(lib_a-init.o): In function `__libc_init_array':
C:\msys\1.0\home\yagarto\newlib-build\arm-none-eabi\thumb\v7m\newlib\libc\misc/../../../../../../../newlib-1.20.0/newlib/libc/misc/init.c:37: undefined reference to `_init'
collect2.exe: error: ld returned 1 exit status

为什么会出现此“ undefined reference ”错误?我想念什么?我以为我缺少一些链接器标志,但是我无法一辈子弄清楚是什么。

最佳答案

我不是专家,但是:

_init(正常的运行时入口点)可能引用了执行ctor和dtor表的代码。

使用-nostartfiles可以避免标准启动,并且--gc-sections可能会消除整个启动代码。显式调用将再次添加引用。

如果省略--gc-sections无法解决问题,则它也可能是(嵌入式)链接程序脚本中缺少的keep()语句,该语句始终保持输入代码或您自己的启动代码
(startup_ *)应该引用它

关于gcc - 为什么在__libc_init_array中有对_init的 undefined reference ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13734745/

相关文章:

gcc - 与 OS X 10.10.2 的 "__mpf_struct"相关的 undefined symbol 错误

c - GCC x86 内联汇编中的 (+r) 与 (=r) 约束

linker - g++链接器无法识别-Bstatic

c - 链接时允许不同的类型

c++ - xerces-c 2.8 : error while loading shared libraries

c - 用 C 和基本库烘焙 PI

macos - nvcc 未知选项 -no_pie

c - 为什么 scanf() 在此代码中导致无限循环?