简体   繁体   English

static库的链接和加载

[英]Linking and Loading of static library

My question is how exactly the linker works.我的问题是 linker 究竟是如何工作的。

  • I am linking an executable with multiple third-party static libraries.我正在将一个可执行文件与多个第三方 static 库链接。 Out of those static libraries, only a few of them are used by the executable.在这些 static 库中,可执行文件仅使用其中的几个。 In the above case, does linker links only to the libraries whose functions are referenced in the executable?在上述情况下,linker 是否仅链接到其函数在可执行文件中引用的库?
  • If a static library has multiple object files and only one is used by executable, does it only links to that object file?如果 static 库有多个 object 文件并且只有一个被可执行文件使用,它是否只链接到该 object 文件? or its links to the whole static library but loads only the object file which is used?或者它指向整个 static 库的链接,但只加载使用的 object 文件?

For your first question if no symbols from a given library are used it will usually not be included in the final product.对于您的第一个问题,如果没有使用给定库中的符号,它通常不会包含在最终产品中。 Regarding object files the linker likely won't even include full object files but only symbols that are actually referenced, though your linker may have flags that change this behavior and cause the entire library to be included. Regarding object files the linker likely won't even include full object files but only symbols that are actually referenced, though your linker may have flags that change this behavior and cause the entire library to be included.

... how exactly the linker works. ... linker 究竟是如何工作的。

a)... does linker links only to the libraries whose functions are referenced in the executable? a)... linker 是否仅链接到可执行文件中引用其功能的库?

b)... static library has multiple object files and only one is used by executable, does it only links to that object file? b)... static 库有多个 object 文件,并且只有一个由可执行文件使用,它是否只链接到该 object 文件?

It depends... on Linux there are two kinds of libraries... ".so", and the.a (archive).这取决于...在 Linux 上,有两种库...“.so”和 the.a(存档)。

example:例子:

 /usr/lib/x86_64-linux-gnu/libgmpxx.a
 /usr/lib/x86_64-linux-gnu/libgmpxx.so

If you specify the.a in the link portion of your build command, only the contained object files referenced by your app will be linked (not the whole library).如果您在构建命令的链接部分指定 .a,则只会链接您的应用程序引用的 object 文件(而不是整个库)。 This executable is 'stand-alone', and every copy running has its own copy of any functions it uses.这个可执行文件是“独立的”,每个运行的副本都有它自己使用的任何功能的副本。

If you specify the.so in the link portion of your build command, and your app is the first to use a particular ".so" lib, I believe your app will be briefly suspended during its start-up while the WHOLE ".so" lib is loaded.如果您在构建命令的链接部分指定.so,并且您的应用程序是第一个使用特定“.so”库的应用程序,我相信您的应用程序将在启动期间短暂暂停,而整个“.so” “ lib 已加载。

If you specify the.so in the link portion of your build command, and your app is not-the-first to use this particular.so, then the loader will add to your app a mapping to the already-loaded-'.so' in system memory.如果您在构建命令的链接部分指定了.so,并且您的应用程序不是第一个使用此特定.so 的,那么加载器将向您的应用程序添加到已经加载的-'.so 的映射' 在系统 memory 中。 (a much faster connection) (更快的连接)

Executable's using.so's rely on the system to have loaded the.so libraries into memory, and to memory-map the library into the app memory and complete the links of the app to the required functions. Executable的using.so依赖系统将.so库加载到memory中,并将库内存映射到app memory中,完成app到所需功能的链接。

I believe your 'static library' corresponds to the use of ".a" (archive) library.我相信您的“静态库”对应于“.a”(存档)库的使用。

a) yes - the linker (sometimes linking-loader) 'finishes' when there are no more unresolved references (to objects or functions). a) 是的 - 当没有更多未解析的引用(对象或函数)时,linker(有时是链接加载器)“完成”。

b) yes - see a) b) 是的 - 见 a)

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

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