为什么指定输入库的顺序很重要?

发布于 2024-12-19 14:18:46 字数 312 浏览 3 评论 0原文

我对 Linux 编程还很陌生。你可以说我是 Windows 人。因此,我将我的项目移植到 Linux,这几乎让我发疯:我确信我已经使用 -l 标志指定了所有依赖项,但我收到了“未解析的符号”错误。 然后我找到了这个主题,它解决了我的问题: Boost linking on Linux with GCC

有人可以解释一下为什么顺序很重要,以及它到底有多重要吗?我很确定 MSVC 链接器不是这种情况......

I'm quite new to programming for Linux. You could say I'm a Windows guy. So, I was porting my project to Linux, and it almost made me insane: I'm sure I have specified all the dependencies with -l flag, and yet I'm getting "unresolved symbol" errors.
Then I've found this topic, and it solved my problem: Boost linking on Linux with GCC

Could someone please explain me why does the order matter, and how exactly it matters? I'm pretty sure it is not the case with MSVC linker...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月蹉跎了容颜 2024-12-26 14:18:46

一个简单的例子将让您了解为什么一次性 Unix 链接器关心顺序。

假设您有 main.o(由 main.cpp 生成)和库 libx.a(无依赖项)和 liby.a(依赖于名为 newRefX 的 libx)

如果链接器按此顺序运行,则可以,因为链接器从 1 到 3:

  1. main.o refX=undef, refY=undef
  2. liby.a refX=undef, refY=def, newRefX=undef
  3. libx.a refX=def, refY=def, newRefX=def

但是如果链接器按照这个顺序,你会遇到问题newRefX:

  1. main.o refX=undef, refY=undef
  2. libx.a refX=def, refY=undef,
  3. liby.a refX=def, refY=def, newRefX=undef

因此,您可以看到您最后需要的是最低级别的库(不依赖于其他库的库)。

A simple example will let you see why one-pass Unix linkers care about order.

Suppose you have main.o (generated by main.cpp) and libraries libx.a (no dependencies) and liby.a ( depends on libx called newRefX).

If the linker goes in this order, you are fine as the linker goes from 1 to 3:

  1. main.o refX=undef, refY=undef
  2. liby.a refX=undef, refY=def, newRefX=undef
  3. libx.a refX=def, refY=def, newRefX=def

But if the linker goes in this order, you run into problems with newRefX:

  1. main.o refX=undef, refY=undef
  2. libx.a refX=def, refY=undef,
  3. liby.a refX=def, refY=def, newRefX=undef

So, you can see that you want the lowest level library (the one that depends on no others) last.

丶情人眼里出诗心の 2024-12-26 14:18:46

来自“GCC 简介 - 针对 GNU 编译器 gcc 和 g++

链接器的传统行为是在命令行指定的库中从左到右搜索外部函数。这意味着包含函数定义的库应出现在使用该函数的任何源文件或目标文件之后。

我相信 msvc 链接器会对代码执行 2 次传递,因此即使以不同的顺序指定库(缺少引用...),它们也可能能够解析符号

From "An Introduction to GCC - for the GNU compilers gcc and g++"

The traditional behavior of linkers is to search for external functions from left to right in the libraries specified on the command line. This means that a library containing the definition of a function should appear after any source files or object files which use it.

I believe that msvc linkers do 2 passes over the code so they might be able to resolve the symbols even when the libraries are specified in different order (reference missing ...)

天涯离梦残月幽梦 2024-12-26 14:18:46

这就是 Unix 链接器的工作方式,从很久以前开始......请参阅 Levine 的书

It is the way the Unix linkers work, since long time ago... See Levine's book

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文