简体   繁体   English

链接动态库

[英]Linking dynamic library

I want to know exactly how the process of linking works in case of dynamic library. 我想确切地知道在动态库的情况下链接过程如何工作。 As I already know it first loads the image/binary in memory and (for every program calling/linking for the library) it's called. 据我所知,它首先将图像/二进制文件加载到内存中,并且(对于每个程序调用/链接库)都被调用。 but my question is the following: how the program distinguishes the shared and static libraries? 但是我的问题是:程序如何区分共享库和静态库? only just by .so and .a? 仅靠.so和.a吗? if I link the full path of the dynamic library like /path/to/so/file.so it sounds silly but does it become static and the content is copied into executable binary? 如果我链接动态库的完整路径(例如/path/to/so/file.so),听起来听起来很傻,但是会变成静态并将内容复制到可执行二进制文件中吗?

Thanks ... 谢谢 ...

No, Providing the linker with a full path doesn't turn the library into a static one. 不,为链接程序提供完整路径不会将库变成静态库。 The "decision" which code will be part of the binary happens at compile time. 哪些代码将成为二进制文件的“决定”在编译时发生。 When the binary is loaded the loader can load additional libraries (dynamic) but they are not part of the binary itself (ie the size of the binary will not change after compilation is done). 加载二进制文件后,加载程序可以加载其他库(动态库),但它们不是二进制文件本身的一部分(即,编译完成后,二进制文件的大小将不会更改)。

The loader can tell the difference between a static and dynamic library by examining the library's object file. 加载程序可以通过检查库的目标文件来区分静态库和动态库。 This file is generated in the compilation process (for example using "-shared" on Linux gcc), ie compiled static libraries has different object file format than dynamically compiled libraries. 该文件在编译过程中生成(例如,在Linux gcc上使用“共享”),即,已编译的静态库与动态编译的库具有不同的目标文件格式。

Providing a full path to the linker is related to the way the linker is searching for the library file for compilation (or later on the loader when it is looking for the shared library on the system). 提供链接程序的完整路径与链接程序搜索库文件进行编译的方式有关(或稍后在加载程序上寻找系统上的共享库时在加载程序上)。 It does not affect the type of the library (which was already "chosen" when it was compiled). 它不会影响库的类型(在编译时已经“选择”了该库)。

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

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