为什么指定输入库的顺序很重要? [英] Why does order in which input libraries are specified matter?

查看:96
本文介绍了为什么指定输入库的顺序很重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Linux编程很陌生.您可能会说我是Windows专家.因此,我将项目移植到Linux,这几乎使我发疯:我确定我已经使用-l标志指定了所有依赖项,但是我遇到了未解析符号"错误. 然后,我找到了这个主题,它解决了我的问题:在Linux上使用GCC增强链接

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

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

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...

推荐答案

一个简单的示例将使您了解为何单次Unix链接程序关心顺序.

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

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

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

如果链接器按此顺序排列,则可以正常进行,因为链接器从1变为3:

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
  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的问题:

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
  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.

这篇关于为什么指定输入库的顺序很重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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