【问题标题】:Compiling using arm-none-eabi-gcc and linking library liba.a error使用 arm-none-eabi-gcc 编译并链接库 liba.a 错误
【发布时间】:2025-12-15 14:25:01
【问题描述】:

我正在 64 位 Linux 机器上用 C 语言编译一个 hello world 程序。我正在使用 GCC ARM 嵌入式工具链在带有 ATMEL AT91SAM9G20 处理器的 FOX G20 V 板上交叉编译我的程序。

第一次编译时,我在编译时遇到了一些错误,因为程序无法识别 printf、return 等函数(标准 C 函数)。所以我决定通过arm-none-eabi-gcc -o hello hello.c libc.a 建立函数之间的链接,我认为这些函数是在 libc.a 库中定义的(如果我错了,请纠正我),但结果仍然会导致错误:

libc.a(lib_a-exit.o): In function `exit':
exit.c:(.text.exit+0x16): undefined reference to `_exit'
libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk'
libc.a(lib_a-writer.o): In function `_write_r':
writer.c:(.text._write_r+0x10): undefined reference to `_write'
libc.a(lib_a-closer.o): In function `_close_r':
closer.c:(.text._close_r+0xc): undefined reference to `_close'
libc.a(lib_a-fstatr.o): In function `_fstat_r':
fstatr.c:(.text._fstat_r+0xe): undefined reference to `_fstat'
libc.a(lib_a-isattyr.o): In function `_isatty_r':
isattyr.c:(.text._isatty_r+0xc): undefined reference to `_isatty'
libc.a(lib_a-lseekr.o): In function `_lseek_r':
lseekr.c:(.text._lseek_r+0x10): undefined reference to `_lseek'
libc.a(lib_a-readr.o): In function `_read_r':
readr.c:(.text._read_r+0x10): undefined reference to `_read'
collect2: error: ld returned 1 exit status

我真的不确定为什么即使我已将库链接到应用程序,程序仍然无法识别标准函数。如果有人有理由说明原因,或者有解决此问题的方法,我将不胜感激。

更新

我已经从here 下载了 glibc 库。我已经从newlib-2.1.0/libgloss/arm 中找到的syscalls.c 库编译并创建了一个静态库,当我将该库链接到我的应用程序代码时,我仍然得到同样的错误。

【问题讨论】:

  • 您使用的是什么 GCC ARM 嵌入式工具链?看起来未定义的引用是您(或某人)期望编写的用于将libc.a 库集成到特定平台的函数。
  • @MichaelBurr 这是 Linero GCC 的预构建 32 位版本。那么有什么我需要添加到libc.a 库中的吗?如果是这样,那会是什么?

标签: c linux gcc arm processor


【解决方案1】:

尝试执行这个:

arm-none-eabi-gcc --specs=rdimon.specs -lgcc -lc -lm -lrdimon -o hello hello.c

您的工具包似乎提供了 librdimon.a,这是一个提供基本标准 C 函数的库。

【讨论】:

    【解决方案2】:

    我收到此错误是因为我的二进制文件无法装入 ROM。

    我的第一个错误是:

    address 0x34000 of arm_flash.elf section `.mmu_tbl' is not within region `ps7_ram_0`
    

    然后我得到了相同的未定义引用错误列表。

    我需要通过从我的 C++ 代码中删除 new 关键字和所有动态内存分配来减小二进制大小。

    【讨论】: