使用 arm-none-eabi-gcc 编译 hello world 程序 C 时出错 [英] Error compiling hello world program C with arm-none-eabi-gcc

查看:52
本文介绍了使用 arm-none-eabi-gcc 编译 hello world 程序 C 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to compile a hello world program in C on a Linux 64-bit machine. I am using an ARM cross compiler to load my application onto an ARM processor. However, when compiling the code using arm-none-eabi-gcc -o hello hello.c I get a series of errors:

/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): 在函数 exit' 中:exit.c:(.text.exit+0x2c):对_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):在函数 _sbrk_r' 中:sbrkr.c:(.text._sbrk_r+0x18): 未定义的引用_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):在函数 _write_r' 中:writer.c:(.text._write_r+0x20): 对_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):在函数 _close_r' 中:close.c:(.text._close_r+0x18): 对_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):在函数 _fstat_r' 中:fstatr.c:(.text._fstat_r+0x1c):对_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):在函数 _isatty_r' 中:isattyr.c:(.text._isatty_r+0x18): 未定义引用_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):在函数 _lseek_r' 中:lseekr.c:(.text._lseek_r+0x20):对_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):在函数 _read_r' 中:readr.c:(.text._read_r+0x20): 对_read'的未定义引用collect2:错误:ld 返回 1 个退出状态

/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,它告诉我编译器运行良好.

When I try compiling by doing: arm-none-eabi-gcc -c hello.c, it creates an object code hello.o which tells me that the compiler runs fine.

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

Could someone perhaps tell me why my compilation is return such errors please?

更新

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

I realize now that the C runtime library isn't included in the compilation. Does anyone know of any options I need to include in the compilation or how to link the library to be able to use standard functions such as printf for example?

推荐答案

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

What should the MCU chip do with a printf string since there is no console? That's what is happening. libc needs low level functions that is platform dependent and shall not be included in the libc.

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

You need a retarget.c or a syscalls.c file that defines all these missing symbols and properly retarget the printf output to a serial line that you have access from the outside of the chip.

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

I strongly recommend you to use the yagarto toolkit, they provide a syscall.c file with what's necessary to build a project when linking against libc, as you are doing.

如果您想尝试短路径,请包含 syscall.c将文件添加到您的项目并重新编程其内部函数以满足您的需要.

If you want to try the short path, include the syscall.c file to your project and reprogram its functions internals to suit your needs.

在微控制器世界中将裸 gcc 工具链与 libc 链接起来不适合初学者.我的建议是始终按照 yagarto 的教程逐步进行操作.

Linking bare gcc toolchain against libc in the microcontroller world is not for beginners. My recommendation is always to follow step by step yagarto's tutorials.

这篇关于使用 arm-none-eabi-gcc 编译 hello world 程序 C 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆