gpt4 book ai didi

eclipse - 使用 Eclipse 的 C 项目上的链接器错误

转载 作者:行者123 更新时间:2023-12-03 10:36:18 26 4
gpt4 key购买 nike

我想为 STM32F217IG 微 Controller 创建一个项目。
所以我安装了 Eclipse 和 GNU for ARM embedded GCC cross compiler .我不认为它是 Code Sourcery 之一。我使用它,因为它支持浮点数而 Code Sourcery 不支持。
一旦我这样做了,我尝试创建一个只有两个源文件的非常小的项目:test.c 和 main.c,并且只用它们编写了以下代码:

#include <stdlib.h>
#include <stdio.h>

int main (void)
{
printf("Hello, World!");
return 0;
}
我更改了项目属性中的行命令,将 GCC 替换为 arm-none-eabi-gcc,然后尝试编译项目。
我自己没有创建任何 makefile;我在 Eclipse 中使用了自动创建。
建筑物似乎很好,但是当涉及到链接器时,我在控制台中收到以下错误:
make all
'Building target: test3'
'Invoking: Cross GCC Linker'
arm-none-eabi-gcc -o"test3" ./main.o ./test3.o

c:/program files/gnu tools arm embedded/4.6 2012q4/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib\libc.a(lib_a-exit.o): In function `exit':
exit.c:(.text.exit+0x2c): undefined reference to `_exit'

c:/program files/gnu tools arm embedded/4.6 2012q4/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib\libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0x18): undefined reference to `_sbrk'

c:/program files/gnu tools arm embedded/4.6 2012q4/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib\libc.a(lib_a-writer.o): In function `_write_r':
writer.c:(.text._write_r+0x20): undefined reference to `_write'

c:/program files/gnu tools arm embedded/4.6 2012q4/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib\libc.a(lib_a-closer.o): In function `_close_r':
closer.c:(.text._close_r+0x18): undefined reference to `_close'

c:/program files/gnu tools arm embedded/4.6 2012q4/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib\libc.a(lib_a-fstatr.o): In function `_fstat_r':
fstatr.c:(.text._fstat_r+0x1c): undefined reference to `_fstat'

c:/program files/gnu tools arm embedded/4.6 2012q4/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib\libc.a(lib_a-isattyr.o): In function `_isatty_r':
isattyr.c:(.text._isatty_r+0x18): undefined reference to `_isatty'

c:/program files/gnu tools arm embedded/4.6 2012q4/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib\libc.a(lib_a-lseekr.o): In function `_lseek_r':
lseekr.c:(.text._lseek_r+0x20): undefined reference to `_lseek'

c:/program files/gnu tools arm embedded/4.6 2012q4/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../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: ld returned 1 exit status

make: *** [test3] Erreur 1
上网查了一下,发现可能是syscall的问题。但我不知道如何将这个库添加到我在 Linux 上的项目中。
这真的是问题吗?如果是,我该如何解决?如果没有,错误来自哪里?
正如有人建议的那样,我尝试“链接”C 运行时库。在 Eclipse 中,我似乎有两种解决方案可以做到:
首先在项目属性 → C/C++ → 构建 → 设置 → 交叉链接器 → 库。我只是添加了字母 c然后错误没有改变,但有 -lc在命令行的末尾:
 make all
'Building target: test3'
'Invoking: Cross GCC Linker'
arm-none-eabi-gcc -o"test3" ./main.o ./test3.o -lc

c:/program files/gnu tools arm embedded/4.6 2012q4/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib\libc.a(lib_a-exit.o): In function `exit':
exit.c:(.text.exit+0x2c): undefined reference to `_exit'

c:/program files/gnu tools arm embedded/4.6 2012q4/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib\libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0x18): undefined reference to `_sbrk'

c:/program files/gnu tools arm embedded/4.6 2012q4/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib\libc.a(lib_a-writer.o): In function `_write_r':
writer.c:(.text._write_r+0x20): undefined reference to `_write'

c:/program files/gnu tools arm embedded/4.6 2012q4/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib\libc.a(lib_a-closer.o): In function `_close_r':
closer.c:(.text._close_r+0x18): undefined reference to `_close'

c:/program files/gnu tools arm embedded/4.6 2012q4/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib\libc.a(lib_a-fstatr.o): In function `_fstat_r':
fstatr.c:(.text._fstat_r+0x1c): undefined reference to `_fstat'

c:/program files/gnu tools arm embedded/4.6 2012q4/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib\libc.a(lib_a-isattyr.o): In function `_isatty_r':
isattyr.c:(.text._isatty_r+0x18): undefined reference to `_isatty'

c:/program files/gnu tools arm embedded/4.6 2012q4/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib\libc.a(lib_a-lseekr.o): In function `_lseek_r':
lseekr.c:(.text._lseek_r+0x20): undefined reference to `_lseek'

c:/program files/gnu tools arm embedded/4.6 2012q4/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../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: ld returned 1 exit status
make: *** [test3] Erreur 1
但是不知道是不是真的要加C运行库。
其次,我在项目属性→C/C++通用→路径和符号→库中添加了libc.a库,这是我得到的(完全不同):
make all
'Building target: test3'
'Invoking: Cross GCC Linker'
arm-none-eabi-gcc -o"test3" ./main.o ./test3.o -l"C:/Program\ Files/GNU\ Tools\ ARM\ Embedded/4.6\ 2012q4/arm-none-eabi/lib/armv7-m/libc.a"

c:/program files/gnu tools arm embedded/4.6 2012q4/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/bin/ld.exe: cannot find -lC:/Program\ Files/GNU\ Tools\ ARM\ Embedded/4.6\ 2012q4/arm-none-eabi/lib/armv7-m/libc.a

collect2: ld returned 1 exit status
make: *** [test3] Erreur 1
然后它仍然不起作用,但它是搜索的好方法吗?
哦,还有一个非常有趣的事实:
我只在 Debug模式下遇到错误。如果我处于 Release模式,一切正常,并且我没有任何错误(除非我添加了 libc.a,否则我认为这不是最好的做法)。这是否意味着问题在于 .elf 文件创建?

最佳答案

我查看了您链接的工具包,只是为了阅读 readme.txt 中的以下内容:

This toolchain is built and optimized for Cortex-R/M bare metal development.



换句话说,这个工具链是专门为嵌入式系统配置的,可能根本没有运行操作系统。在这种情况下,没有系统可以提供例如标准输出其中 printf()应该写,没有文件系统等 - 如果你想使用它们,你必须链接到一些提供这些基本服务的库。

也就是说,您的工具包提供 librdimon.a提供所有这些基本服务的图书馆。这个库实际上是 libgloss 的一部分汇编。如果要链接它,请尝试以下命令:
arm-none-eabi-gcc --specs=rdimon.specs   -Wl,--start-group -lgcc -lc -lm -lrdimon -Wl,--end-group -o test test.c

这在我的 PC 上链接良好,但它是否是您真正想要的则是另一回事(无论如何,您希望在哪里看到 printf() 输出?)。

对于您的芯片,您可能应该寻找一个将标准输出重定向到串行端口或通过 JTAG 提供调试输出的库。或者,您可以使用自定义功能,例如将调试输出发送到串行控制台而不是 printf() - 由你决定。
如果您决定使用 printf()我建议阅读 libgloss文档。

另外,我建议四处寻找专门为 STM32 系列捆绑的工具链。正确配置所有这些基本内容(C 库、链接描述文件等)需要一些经验。

编辑:许多嵌入式系统并没有真正使用标准的 C 库,几乎都是从头开始。如果你想走这条路,你应该通过 -nostdlib给您的 gcc调用。当然,您将不再拥有 printf() 之类的东西。可用的。

编辑 2:另一种方法是使用标准库( newlib ,我的意思是)而不使用 libgloss并为您不需要的东西提供适当的 stub 。您可能想关注 this tutorial ,其中 _read_write是通过串行端口实现的,其他一切都是 stub 。这很可能是您真正想要的。

关于eclipse - 使用 Eclipse 的 C 项目上的链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13235748/

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