简体   繁体   English

static 和共享库及其交叉链接

[英]static and shared library and their cross linking

After reading a lot of threads, I am still confused regarding shared and static library and the way how they can be cross linked.在阅读了很多线程之后,我仍然对共享库和 static 库以及它们如何交叉链接的方式感到困惑。

So my understanding is that when I have an executable that links to a static lib, the executable copies full contents of the functions/resources it needs into itself, so when it runs, it no longer needs the static lib.所以我的理解是,当我有一个链接到 static 库的可执行文件时,可执行文件会将其所需的函数/资源的全部内容复制到自身中,因此当它运行时,它不再需要 static 库。 If the executable links to a shared lib, then it only create place holder for all the contents it needs, so when it runs, it needs the shared lib in the path, so it can loads the contents to the placeholder.如果可执行文件链接到一个共享库,那么它只会为它需要的所有内容创建占位符,因此当它运行时,它需要路径中的共享库,因此它可以将内容加载到占位符。

If the above statement is correct, then are the following correct?如果上面的说法是正确的,那么下面的说法是否正确?

  1. when I build a static library A that links to a shared lib B , and then build an executable C that links to A for functions that are originally from B .当我构建一个链接到共享库B的 static 库A ,然后构建一个可执行的C链接到A以获取最初来自B的函数时。 As a result, when I run C , I don't A to be presented because all needed contents are already in C , but B is still needed.结果,当我运行C时,我不会A ,因为所有需要的内容都已经在C中,但仍然需要B
  2. when I build a shared library A that links to a static lib B , and then build an executable C that links to A for functions that are originally from B .当我构建一个链接到 static lib B的共享库A ,然后构建一个可执行的C链接到A以获取最初来自B的函数时。 As a result, when I run C , I only need A but not B as all the contents needed is already in A .结果,当我运行C时,我只需要A而不是B因为所需的所有内容都已经在A中。

when I have an executable that links to a static lib, the executable copies full contents of the functions/resources it needs into itself, so when it runs, it no longer needs the static lib当我有一个链接到 static 库的可执行文件时,可执行文件会将其所需的函数/资源的全部内容复制到自身中,因此当它运行时,它不再需要 static 库

Correct.正确的。

If the executable links to a shared lib, then it only create place holder for all the contents it needs, so when it runs, it needs the shared lib in the path如果可执行文件链接到共享库,那么它只会为它需要的所有内容创建占位符,所以当它运行时,它需要路径中的共享库

It needs access to the shared library, certainly.当然,它需要访问共享库。 The rules by which the shared library is found varies by platform.找到共享库的规则因平台而异。

when I build a static library A that links to a shared lib B, and then build an executable C that links to A for functions that are originally from B. As a result, when I run C, I don't A to be presented because all needed contents are already in C, but B is still needed.当我构建一个链接到共享库 B 的 static 库 A,然后构建一个可执行的 C 链接到 A 以获取最初来自 B 的函数时。结果,当我运行 Z0D61F8370CAD1D412F80B84D1因为所有需要的内容都已经在 C 中,但仍然需要 B。

Correct正确的

when I build a shared library A that links to a static lib B, and then build an executable C that links to A for functions that are originally from B. As a result, when I run C, I only need A but not B as all the contents needed is already in A当我构建一个链接到 static 库 B 的共享库 A,然后构建一个可执行的 C 链接到 A 以获取最初来自 B 的函数时。结果,当我运行 Z0D61F8370CAD1D3E B12F80B84D1 而不是 A所有需要的内容都已经在A中

Correct正确的

Essentially, when you link against a static library, you never need it at runtime, and when you link against a shared library, you do.本质上,当您链接到 static 库时,您在运行时永远不需要它,而当您链接到共享库时,您需要它。

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

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