为什么我必须在链接器行的末尾传递库? [英] Why do I have to pass libraries in the end of linker line?

查看:77
本文介绍了为什么我必须在链接器行的末尾传递库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样的命令将起作用:

(LD) $(LDFLAGS) -o reip.app newlib/crt0.o reip.o renetif.o fs.o httpd.o liblwip.a newlib/libc.a

但是,如果我将* .a放在* .o文件之前-它将无法从库中找到函数.

But if I place *.a before *.o files - it will be not able to find functions from libs.

我已经搜索了一些旧项目,并在如下所示的makefiles行中找到了

I've searched through some old projects and found in makefiles lines like these:

$(CC)  $(LIBS) -o $(TARGET) JukeboxMain.o JukeboxPlayer.o ...

在我将$(LIBS)移至行尾之前均无效.但是我记得很早以前它对我有用.有人可以回答更改的内容以及为什么它不再起作用了)P.S.我不太喜欢使用编译器,链接器和其他工具-我更喜欢创建一些东西;)

which doesn't work either until I move $(LIBS) to the end of line. But I remember it was working for me long time ago. Could someone please answer what changed and why it doesn't work anymore ) P.S. I'm not a big fan of playing with compilers, linkers and other tools - I mostly prefer to create something ;)

推荐答案

对于您为什么认为以前可行,我无法回答.但我可以告诉您为什么它通常无法正常工作.大多数 POSIX 风格的链接器,包括在 GNU/Linux 和其他系统上使用的 GNU binutils ld,都是单程链接器.

I can't answer you as to why you think it might have worked before. But I can tell you why it doesn't work in general. Most POSIX-style linkers, which includes the GNU binutils ld using on GNU/Linux and others, are single-pass linkers.

这意味着他们会从头到尾遍历命令行中列出的元素,并且不会返回.链接器启动时,它基本上是在寻找一个符号 main .当它在命令行中移动时,链接器找到的任何目标文件( foo.o )始终添加到可执行文件中.该对象提供已定义符号的列表和未定义(其他对象需要)的列表.

That means they walk the elements listed in the command line from the start to the end one time and they don't go back. When the linker starts it's basically looking for one symbol, main. As it walks the command line, any object file (foo.o) the linker finds is always added to the executable. The object provides a list of symbols which are defined, and a list which are undefined (needed from other objects).

当链接器访问静态库( libfoo.a )时,它会浏览该库中的目标文件,并且如果对象包含链接器所需的任何符号,则会将该对象添加到可执行文件.如果它不包含链接程序所需的任何符号,则会跳过该对象.

When the linker hits a static library (libfoo.a), it looks through the object files in that library and if an object contains any symbol which the linker needs, it adds that object to the executable. If it does not contain any symbol the linker needs, it skips that object.

在命令行末尾,如果需要但未找到任何符号,则链接失败.

At the end of the command line, if any symbol was needed but not found, then the link fails.

因此,您可以看到为了确保一次就能找到所有内容,来自库的NEED符号必须在提供这些符号的库之前 出现,否则链接程序单次访问该库时,它不会知道是否需要这些库,因此会跳过它们.

So, you can see that in order to be sure everything is found in a single pass, the things that NEED symbols from libraries must come before the libraries that provide those symbols, or else when the linker hits the library in its single pass it won't know that the libraries are needed and will skip them.

这篇关于为什么我必须在链接器行的末尾传递库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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