g ++:静态和动态库应该以什么顺序链接? [英] g++: In what order should static and dynamic libraries be linked?

查看:230
本文介绍了g ++:静态和动态库应该以什么顺序链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个名为my_app的主要可执行文件,它使用了几个其他的库:3个库静态链接,其他3个动态链接。
它们应该与my_app链接到哪个顺序?



但是这些链接顺序应该是什么?



让我们假设我们得到了取决于libSB的libSA(如在静态A中)和取决于libSB的libSC:

  libSA  - > libSB  - > libSC 

和三个动态库: libDA - > libDB - > libDC libDA 是基本的, libDC 是最高的)



在哪些顺序应该链接?基本的第一个还是最后一个?

  g ++ ... -g libSA libSB libSC -lDA -lDB -lDC -o my_app 

看起来像是正确的顺序,但是是这样吗?如果在任何动态库和静态库之间存在依赖关系,或者其他方式如何?

解决方案

没有什么关系,因为你实际上并不链接静态库 - 你所做的就是把一些目标文件在一起存档。所有你需要编译你的目标文件,你可以立即创建静态库。



动态库的情况更复杂,有两个方面: p>


  1. 共享库的工作方式与静态库完全相同(除了共享段,如果它们存在)可以只是做同样的 - 只要链接你的共享库,只要你有目标文件。这意味着例如libDA中的符号将在libDB中显示为未定义


  2. 在链接共享对象时,可以在命令行中指定要链接的库。这与1.有相同的效果,但是,将libDB标记为需要libDA。


使用前一种方式,在链接可执行文件时,必须在命令行中指定所有三个库(-lDA,-lDB,-lDC)。如果使用后者,您只需指定-lDC,它将在链接时自动拉取其他。请注意,链接时间就在你的程序运行之前(这意味着你可以得到不同版本的符号,甚至从不同的库)。



这一切都适用于UNIX; Windows DLL的工作方式完全不同。



在解释问题后编辑:



c> ld 信息手册。


链接器将仅搜索
一次,在命令行上指定
的位置。如果
存档定义一个符号,
未定义在
命令行上的存档之前出现的某个对象中,链接器将包括
适当的文件s)从
归档。但是,稍后在
命令行中出现的对象中未定义的符号
不会使链接器
再次搜索归档。



查看` - ('选项,强制
链接器搜索多个
次。



在命令行中存档多个
次。



这种类型的归档搜索是Unix链接器的
标准,但如果
在AIX上使用`ld',请注意
它不同于
AIX链接器的行为。


这意味着:



任何依赖于其他库的静态库或对象都应放置在命令行之前,如果静态库循环依赖,可以使用 - ()命令行选项,或者在命令行上放置两次库( -lDA -lDB -lDA )。动态库的顺序无关紧要。


Let's say we got a main executable called "my_app" and it uses several other libraries: 3 libraries are linked statically, and other 3 are linked dynamically. In which order should they be linked against "my_app"?

But in which order should these be linked?

Let's say we got libSA (as in Static A) which depends on libSB, and libSC which depends on libSB:

libSA -> libSB -> libSC

and three dynamic libraries:libDA -> libDB -> libDC (libDA is the basic, libDC is the highest)

in which order should these be linked? the basic one first or last?

g++ ... -g libSA libSB libSC -lDA -lDB -lDC -o my_app

seems like the currect order, but is that so? what if there are dependencies between any dynamic library to a static one, or the other way?

解决方案

In the static case, it doesn't really matter, because you don't actually link static libraries - all you do is pack some object files together in one archive. All you have to is compile your object files, and you can create static libraries right away.

The situation with dynamic libraries is more convoluted, there are two aspects:

  1. A shared library works exactly the same way as static library (except for shared segments, if they are present), which means, you can just do the same - just link your shared library as soon as you have the object files. This means for example symbols from libDA will appear as undefined in libDB

  2. You can specify the libraries to link to on the command line when linking shared objects. This has the same effect as 1., but, marks libDB as needing libDA.

The difference is that if you use the former way, you have to specify all three libraries (-lDA, -lDB, -lDC) on the command line when linking the executable. If you use the latter, you just specify -lDC and it will pull the others automatically at link time. Note that link time is just before your program runs (which means you can get different versions of symbols, even from different libraries).

This all applies to UNIX; Windows DLL work quite differently.

Edit after clarification of the question:

Quote from the ld info manual.

The linker will search an archive only once, at the location where it is specified on the command line. If the archive defines a symbol which was undefined in some object which appeared before the archive on the command line, the linker will include the appropriate file(s) from the archive. However, an undefined symbol in an object appearing later on the command line will not cause the linker to search the archive again.

See the `-(' option for a way to force the linker to search archives multiple times.

You may list the same archive multiple times on the command line.

This type of archive searching is standard for Unix linkers. However, if you are using `ld' on AIX, note that it is different from the behaviour of the AIX linker.

That means:

Any static library or object that depends on other library should be placed before it in the command line. If static libraries depend on each other circularly, you can eg. use the -( command line option, or place the libraries on the command line twice (-lDA -lDB -lDA). The order of dynamic libraries doesn't matter.

这篇关于g ++:静态和动态库应该以什么顺序链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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