简体   繁体   English

C ++静态库

[英]C++ Static Libraries

Aside from inclusion of 3rd party software, why would you make a static library for a project. 除了包含第三方软件外,为什么还要为项目创建静态库。 If your writing the source yourself you could just build it as a part of the project and if it's a library to be used more than once wouldn't it make more sense to dynamically link and sit on a run-time library? 如果您自己编写源代码,则可以将其作为项目的一部分进行构建,并且如果它是一个可以多次使用的库,那么动态链接并位于运行时库上是否更有意义?

Dynamic libraries have a run-time cost due to relocations† because the base and relative load address of the library is unknown until run-time. 动态库由于重定位而具有运行时成本†,因为直到运行时库的基址和相对装载地址都是未知的。 That is, the function calls and variable access to dynamic libraries are indirect. 也就是说,函数调用和对动态库的变量访问是间接的。 For this reason the code for shared libraries must be compiled as position-independent code ( -fPIC flag in gcc ). 因此,必须将共享库的代码编译为与位置无关的代码( gcc -fPIC标志)。

Whereas with static libraries it can use cheaper program counter relative access even with address-space randomization because the relative position of that static library (object files really) is available to the linker. 而使用静态库,即使使用地址空间随机化,它也可以使用便宜的程序计数器相对访问权限,因为链接器可以使用该静态库(实际上是目标文件)的相对位置。

Note that calls to virtual functions are resolved through the vtable (which the dynamic linker can patch on load), so that the cost of calling a virtual function is always the same regardless of where that function resides. 请注意,对虚拟函数的调用是通过vtable(动态链接程序可以在加载时修补该vtable)解决的,因此,无论该函数位于何处,调用虚拟函数的成本始终相同。 (IIRC, I may need to double-check this statement). (IIRC,我可能需要仔细检查此声明)。

See How To Write Shared Libraries by Ulrich Drepper for full details. 有关完整详细信息,请参见Ulrich Drepper的“如何编写共享库”


Linking to shared libraries is easier though because they contain a list of other shared libraries they depend upon. 链接到共享库更容易,因为它们包含它们依赖的其他共享库的列表。

Whereas when linking against a static library one must also link explicitly the dependencies of that static library (because a .a is just a bunch of .o files). 而链接到静态库时,还必须显式链接该静态库的依赖项(因为.a只是一堆.o文件)。

A build system should do extra handling for static libraries so that the user does not have to list static library dependencies every time when linking it. 构建系统应该对静态库进行额外的处理,以便用户不必在每次链接静态库时都列出静态库依赖项。

When linking against a static library the linker only pulls in those .o files from the .a that resolve any unresolved symbols, whereas an entire shared library is loaded at run-time. 当链接静态库时,链接器仅从.a中提取可解析任何未解析符号的.o文件,而在运行时将加载整个共享库。 So that if you have a global object in a .o with constructor/destructor side-effects, those side effects will not happen with a static library unless that global object is linked in. Extra care must be taken to make sure that global object is always linked in . 因此,如果您在.o有一个带有构造函数/析构函数副作用的全局对象,则除非链接了该全局对象,否则静态库不会发生这些副作用。 必须格外小心,以确保全局对象为始终链接

When linking against a shared librarie residing in a non-standard location, along with -L<path> one must specify -Wl,-rpath=<path> as well for the run-time linker to find the shared library there and/or use -Wl,-rpath=$ORIGIN if the shared library is shipped with the executable. 当链接到位于非标准位置的共享库时,必须与-L<path>一起指定-Wl,-rpath=<path> ,以便运行时链接程序在此处查找共享库和/或如果共享库与可执行文件一起提供,请使用-Wl,-rpath = $ ORIGIN Having to set LD_LIBRARY_PATH is a wrong way . 必须设置LD_LIBRARY_PATH是错误的方法


What is PLT/GOT? 什么是PLT / GOT?

The use of dynamic libraries has three main advantages: a) When you release an update of your app it can live in a DL, which is smaller for downloading from Internet than the whole app. 使用动态库具有三个主要优点:a)发布应用程序更新时,它可以存在于DL中,从Internet下载的DL比整个应用程序小。 b) If your app is a great RAM eater, then you can load and unload DL as needed. b)如果您的应用程序是大量的RAM消耗者,那么您可以根据需要加载和卸载DL。 c) Its obvious purpose: share the same code in different apps, in a machine with low resources. c)它的明显目的:在一台资源不足的机器上,在不同的应用程序中共享相同的代码。

a) May lead to dll hell , where different files, same or different versions, populate the directory tree and mess what app uses what .dll a)可能导致dll地狱 ,其中不同的文件(相同或不同的版本)填充目录树,并弄乱了什么应用程序使用什么.dll

b) Is only possible if you reserve an excesive amount of stack RAM. b)仅当您保留大量堆栈RAM时才有可能。 Likely bad design. 可能是糟糕的设计。

c) This may be right for broad used libs, like stdio , drivers, and most of OS helpers. c)这可能适用于广泛使用的库,例如stdio ,驱动程序和大多数OS帮助程序。

The usage of static libraries avoids a) and b). 静态库的使用避免了a)和b)。 The disadvantage are that they make the final executable bigger and that, when code changes, they require likely a full re-compilation of the project 缺点是它们使最终的可执行文件更大,并且当代码更改时,它们可能需要对项目进行完全重新编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM