[英]Mandatorily Dynamically linked libraries
我从书( 专家C编程:深层C秘密,Peter Van Der Linden )中了解到,有些特定的库需要动态链接;哪些是这些库,以及它们为什么强制动态链接?(更具体地说,是GNU / Linux系统)
Which are these libraries
哪些是这些库
All UNIX systems guarantee backward compatibility; 所有UNIX系统都保证向后兼容; that is, a binary built on an older system will continue to work and newer system.
也就是说,在旧系统上构建的二进制文件将继续工作并且更新的系统。 But this guarantee is only made for binaries which link with system libraries dynamically .
但是这种保证仅适用于动态链接系统库的二进制文件。
why they are mandatorily dynamically linked
他们为什么强制动态联系
The restriction is in place because user-level programs generally do not make direct system calls, but rather call libc wrapper routines. 限制已经到位,因为用户级程序通常不进行直接系统调用,而是调用libc包装程序。 So a UNIX vendor is free to make incompatible changes to the syscall interface (eg to fix a bug), provided the system library is updated as well.
因此,如果系统库也更新,那么UNIX供应商可以自由地对syscall接口进行不兼容的更改(例如修复错误)。 Usually such changes only happen when upgrading to new OS release, eg from
Solaris
2.6 to 2.7. 通常,这种更改仅在升级到新的OS版本时发生,例如从
Solaris
2.6到2.7。
The picture on Linux is even more complicated than what I described above, because a single version of glibc
is comprised of some 200+ separate binaries, which all must match exactly . Linux上的图片比我上面描述的要复杂得多,因为单个版本的
glibc
由大约200多个单独的二进制文件组成,所有二进制文件都必须完全匹配。 Linking one such piece in statically, and then running on a system where other pieces do not match will produce unpredictable results; 静态链接一个这样的部分,然后在其他部分不匹配的系统上运行将产生不可预测的结果; often a crash somewhere in
libc
. 经常在
libc
某处崩溃。
Executive summary: never link UNIX system libraries statically into your executables, unless you know what you are doing and have a very good reason to do that. 内容提要: 从来没有静态链接UNIX系统库到你的可执行文件,除非你知道自己在做什么,有一个很好的理由这样做。
POSIX allows whether the dlopen
and dlsym
functions work as desired to be dependent on implementation-defined build conditions, and usually these conditions are either that the program must be dynamic-linked, or that if it's static-linked, that the equivalent of the -rdynamic
linker option be used. POSIX允许
dlopen
和dlsym
函数按需要依赖于实现定义的构建条件,并且这些条件通常是程序必须是动态链接的,或者如果它是静态链接的,则相当于-rdynamic
链接器选项。 So it's very possible that some libraries that depend on dynamically loading modules may only work in dynamic-linked programs, depending on your OS. 因此,依赖于动态加载模块的某些库很可能只能在动态链接程序中工作,具体取决于您的操作系统。
Aside from that, as long as you obey the requirements of the standards for a conforming program, there is no good reason static linking should not work with any library you want. 除此之外, 只要您遵守符合程序标准的要求,静态链接就不适用于您想要的任何库。 If you start relying on hacks that replace standard functions with your own functions by the same name, then behavior may differ between static- and dynamic-linked versions of the same program.
如果您开始依赖使用相同名称替换标准函数和自己的函数的hack,那么同一程序的静态和动态链接版本之间的行为可能会有所不同。 This is one manifestation of undefined behavior .
这是未定义行为的一种表现形式。
It should also be noted that glibc has a number of issues with static linking. 还应该注意的是,glibc在静态链接方面存在许多问题。 Even when static-linked, programs using glibc dynamically load the
libnss_*.so
libraries for processing passwd file/NIS/DNS lookups/etc. 即使在静态链接时,使用glibc的程序也会动态加载
libnss_*.so
库用于处理passwd文件/ NIS / DNS查找/等。 There's also periodic breakage of static linking support in glibc. glibc中还会定期断开静态链接支持。 For instance I recently encountered failures in a glibc function that needed to know the pid/tid due to the thread descriptor for the main thread not being properly initialized in a static linked binary.
例如,我最近在glibc函数中遇到了故障,需要知道pid / tid,因为主线程的线程描述符未在静态链接二进制文件中正确初始化。 If you want to use static linking on Linux, I would highly recommend choosing a non-glibc libc.
如果你想在Linux上使用静态链接,我强烈建议你选择一个非glibc libc。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.