gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 12:32:21 31 4
gpt4 key购买 nike

我正在尝试在 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com