使用Eclipse的一个C项目链接错误 [英] Linker error on a C project using eclipse

查看:921
本文介绍了使用Eclipse的一个C项目链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个STM32F217IG微控制器的一个项目。

I want create a project for an STM32F217IG microcontroller.

然后我安装Eclipse和 GNU的ARM嵌入式GCC交叉编译器。我不认为这是code的Sourcery之一,我用它,因为它支持浮点和code的Sourcery没有。

Then I installed eclipse and the GNU for ARM embedded gcc cross compiler. I don't think it is the Code sourcery one, I used it because it supports floating point and the code sourcery does not.

一旦我做到了我想只有两个源文件创建一个非常小的项目:test.c的和main.c中只写在他们两人的:

Once I did it I tried creating a really small project with only two sources files: test.c and main.c with only written in both of them:

#include <stdlib.h>
#include <stdio.h>
int main (void)
{
    printf("hellowolrd");
    return 0;

}

我在项目财产臂无 - EABI - 海合会取代GCC改了行命令,然后尝试编译该项目。

I changed the line command in the project property to replace gcc by arm-none-eabi-gcc and then tried to compile the project.

我没有创建任何文件做我自己,我曾经在Eclipse中自动创建。

I did not create any make file myself, I used the automatic creation in eclipse.

该建筑似乎要被罚款,但是当涉及到​​的链接我在控制台以下错误:

The building seems to be fine but when it comes to the linker I got the following errors in the console:

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

我看着在互联网上,我发现,它可能是一个系统调用的问题。但我不知道我怎么这个库添加到我的Linux项目。

I looked on the internet and I found that it may be a syscall problem. But I don't know how do I add this library to my project on linux.

是不是真的呢?如果是的话我如何解决?如果没有哪里是从哪里来的?

Is it really it? If yes how do i fix it? And if not where do that come from?

修改

至于有人建议,我想链接C运行时库。在Eclipse上看来我有两个解决方案,做到这一点:
首先在项目属性 - > C / C ++建设 - >设置 - >十字接头 - >库我只是添加字母C,然后错误不改但在命令行的末尾-lc:

As someone suggested, I tried "linking" the c runtime library. On eclipse it seems I have two solutions to do it: first on the project properties->C/C++ build->Settings-> Cross linker->libraries I just add the letter c and then the error does not change but there is -lc at the end of the command line:

 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运行时库

But I don't know if it is really what it means to add the C runtime library

其次,我加libc.a库中的项目属性 - > C / C ++常规 - >路径和符号 - >库,这里是我所得到的(完全不同):

Second I added the libc.a library in the project properties->C/C++ general->Path and symbols -> Libraries and here is what I get (completely different):

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

然后,它仍然无法正常工作,但它搜索的好方法吗?

Then it still does not work but is it the good way to search on?

呵呵,很interresting的事实:
我只在调试模式下得到了错误,如果我在释放模式一切都还好,我没有任何错误(execpt如果我添加的libc.a话,我认为这不是做一件好事)。
确实,这意味着问题是为.elf文件创建

Oh and very interresting fact: I got the errors only in debug mode, If I am in release mode everything is okay and I don't have any errors (execpt if I add the libc.a then I think this is not the good thing to do). Does that means the problem is the .elf file creation?

推荐答案

我看了你链接的工具,只是读了 readme.txt文件以下

I've looked at the toolkit you linked, just to read the following in the readme.txt:

此工具链和构建用于Cortex-R / M裸机开发进行了优化。

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

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

In other words, this toolchain is specifically configured for embedded systems, perhaps with no operating system running at all. In such case, there's no system to provide e.g. standard output where printf() is supposed to write, there's no file system, etc. - you have to link against some library providing these basic services, if you want to use them.

这是说,你的工具包提供 librdimon.a 库提供所有这些基本服务。该库实际上是 libgloss 编译的一部分。如果你想针对其链接,请尝试以下命令:

That said, your toolkit provides librdimon.a library which provides all those basic services. This library is actually a part of libgloss compilation. If you want to link against it, try the following command:

arm-none-eabi-gcc --specs=rdimon.specs   -Wl,--start-group -lgcc -lc -lm -lrdimon -Wl,--end-group -o test test.c

此链接罚款我的电脑上,而是它是否是你真正想要的是另外一个故事(你在哪里希望看到的printf()输出呢?)。

This links fine on my PC, but whether it's what you really want is another story (where do you expect to see printf() output, anyway?).

有关你凑钱,你或许应该寻找被重定向标准输出到串行端口或通过JTAG调试提供输出库。或者,也可以如使用自定义功能发送调试输出到串行控制台,而不是的printf() - 这取决于你。
如果您决定使用的printf()我建议您阅读 libgloss 文件。

For you chip you should probably look for a library which redirects the standard output to serial port or provides debug output over JTAG. Alternatively, you can use your custom function e.g. sending debug output to serial console instead of printf() - it's up to you. If you decide on using printf() I suggest reading libgloss documentation.

另外,我建议四处寻找这是专门为捆绑STM32系列工具链。正确配置所有这些基本的东西(C库,链接脚本等),需要一点点的经验。

Also, I suggest looking around for a toolchain which is specifically bundled for STM32 family. Properly configuring all this basic stuff (C library, linker script, etc.) requires a bit of experience.

编辑:许多嵌入式系统并没有真正使用标准C库,从pretty从头开始了。如果你想要走这条路,你应该通过 -nostdlib 你的 GCC 调用。当然,你将不再有类似的事情的printf()可用。

Many embedded systems doesn't really use standard C library, starting pretty much from scratch. If you want to go this way, you should pass -nostdlib to your gcc invocation. Of course, you'll no longer have things like printf() available.

编辑2:另一种方法是使用标准库( newlib ,我的意思),而不 libgloss 并为东西你并不需要相应的存根。您可能要遵循<一个href=\"https://sites.google.com/site/stm32discovery/open-source-development-with-the-stm32-discovery/getting-newlib-to-work-with-stm32-and-$c$c-sourcery-lite-eabi\">this教程,其中 _read _write 通过串行端口实现的,其他一切都存根。这很可能是你真正想要的。

Edit 2: Another way is to use standard library (newlib, I mean) without libgloss and provide appropriate stubs for things you do not need. You might want to follow this tutorial, where _read and _write is implemented over the serial port, and everything else is stubbed. This is most likely what you really want.

这篇关于使用Eclipse的一个C项目链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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