为什么我在__libc_init_array中有_init的未定义引用?

Mar*_*ark 7 linker gcc yagarto

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

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) { ; }
}
Run Code Online (Sandbox Code Playgroud)

除非我注释掉调用__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
Run Code Online (Sandbox Code Playgroud)

为什么我收到此"未定义引用"错误?我错过了什么?我假设有一些我错过的链接器标志,但我不能为我的生活找出什么.

小智 6

老问题,但我遇到了类似的问题,解决方案就像Marco van de Voort所指出的那样,如果您打算使用__libc_init_array,则应省略-nostartfiles链接器选项,以包含正常的libc init函数.答案重复.

其次我建议--specs=nano.specs在与gcc-arm链接时包含标志(我相信yargarto是一个fork甚至只是gcc-arm的预编译),因为它减少了libc等代码消耗.


Mar*_*ort 5

我不是专家,但是:

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

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

如果省略--gc-sections没有解决它,它也可能是(嵌入式)链接描述文件中缺少keep()语句,它始终保留条目代码,或者您自己的启动代码(startup_*)应该引用它