静态和共享库及其交叉链接

static and shared library and their cross linking

读了很多帖子后,我仍然对共享库和静态库以及它们如何交叉链接的方式感到困惑。

所以我的理解是,当我有一个链接到静态库的可执行文件时,可执行文件将它需要的 functions/resources 的全部内容复制到自身中,所以当它 运行s 时,它不再需要静态库。如果可执行文件链接到一个共享库,那么它只为它需要的所有内容创建占位符,所以当它 运行s 时,它需要路径中的共享库,所以它可以将内容加载到占位符.

如果以上说法正确,那么以下说法是否正确?

  1. 当我构建一个链接到共享库 B 的静态库 A,然后构建一个链接到 A 的可执行文件 C 时,函数是最初来自 B。结果,当我运行C时,我没有A被呈现,因为所有需要的内容都已经在C,但B仍然需要。
  2. 当我构建一个链接到静态库 B 的共享库 A,然后构建一个链接到 A 的可执行文件 C 时,函数是最初来自 B。结果,当我运行 C时,我只需要A而不需要B,因为所有需要的内容都已经在A.[=37=中了]

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

正确。

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

当然,它需要访问共享库。共享库的查找规则因平台而异。

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.

正确

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

正确

本质上,当您 link 针对静态库时,您在运行时永远不需要它,而当您 link 针对共享库时,您需要。