c - 使用 arm-none-eabi-gcc 编译 hello world 程序 C 时出错

标签 c linux gcc arm cross-compiling

我正在尝试在 Linux 64 位机器上用 C 语言编译一个 hello world 程序。我正在使用 ARM 交叉编译器将我的应用程序加载到 ARM 处理器上。但是,当使用 arm-none-eabi-gcc -o hello hello.c 编译代码时,出现了一系列错误:

/home/dico/gcc-arm-none-eabi-4_7-2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/libc.a(lib_a-exit.o): In function exit': exit.c:(.text.exit+0x2c): undefined reference to_exit' /home/dico/gcc-arm-none-eabi-4_7-2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/libc.a(lib_a-sbrkr.o): In function _sbrk_r': sbrkr.c:(.text._sbrk_r+0x18): undefined reference to_sbrk' /home/dico/gcc-arm-none-eabi-4_7-2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/libc.a(lib_a-writer.o): In function _write_r': writer.c:(.text._write_r+0x20): undefined reference to_write' /home/dico/gcc-arm-none-eabi-4_7-2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/libc.a(lib_a-closer.o): In function _close_r': closer.c:(.text._close_r+0x18): undefined reference to_close' /home/dico/gcc-arm-none-eabi-4_7-2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/libc.a(lib_a-fstatr.o): In function _fstat_r': fstatr.c:(.text._fstat_r+0x1c): undefined reference to_fstat' /home/dico/gcc-arm-none-eabi-4_7-2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/libc.a(lib_a-isattyr.o): In function _isatty_r': isattyr.c:(.text._isatty_r+0x18): undefined reference to_isatty' /home/dico/gcc-arm-none-eabi-4_7-2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/libc.a(lib_a-lseekr.o): In function _lseek_r': lseekr.c:(.text._lseek_r+0x20): undefined reference to_lseek' /home/dico/gcc-arm-none-eabi-4_7-2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/libc.a(lib_a-readr.o): In function _read_r': readr.c:(.text._read_r+0x20): undefined reference to_read' collect2: error: ld returned 1 exit status

当我尝试通过以下方式进行编译时:arm-none-eabi-gcc -c hello.c,它会创建一个目标代码 hello.o,它告诉我编译器运行良好。

有人能告诉我为什么我的编译会返回这样的错误吗?

更新

我现在意识到 C 运行时库未包含在编译中。有谁知道我需要在编译中包含的任何选项或如何链接库以便能够使用标准函数(例如 printf)?

最佳答案

既然没有控制台,MCU 芯片应该如何处理 printf 字符串?这就是正在发生的事情。 libc 需要依赖于平台的低级函数,不应包含在 libc 中。

您需要一个 retarget.c 或 syscalls.c 文件来定义所有这些缺失的符号,并正确地将 printf 输出重定向到您可以从芯片外部访问的串行线路。

我强烈建议您使用 yagarto工具包,他们提供了一个 syscall.c文件包含在链接 libc 时构建项目所需的内容,就像您正在做的那样。

如果您想尝试短路径,请包含 syscall.c文件到您的项目并重新编程其内部功能以满足您的需求。

在微 Controller 世界中将裸 gcc 工具链与 libc 链接起来不适合初学者。我的建议是始终遵循 yagarto 的教程一步一步。

关于c - 使用 arm-none-eabi-gcc 编译 hello world 程序 C 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23082990/

相关文章:

c - 在编译时设置 union 的值

c - linux中的ls命令使用哪个linux系统调用来显示文件夹/文件名?

c++ - 使用 C 而不是 C++ 编译头文件

linux - 如何在 awk 操作中增加 shell 变量

optimization - LLVM 和优化的 future

c - 海湾合作委员会优化?

C 全局匿名结构/union

linux - 使用wget从Zenodo下载大文件(15G)时出现“ERROR 429: Too Many Requests.”

c++ - 有没有类似conio.h的库?

c++ - 为什么 gcc 与 clang 相比会产生这种奇怪的程序集?