简体   繁体   English

运行时动态库与静态库

[英]dynamic library vs static library at runtime

If I have a project, where I have two modules A and B , and they both link with a third library C . 如果我有一个项目,那么我有两个模块AB ,它们都与第三个库C链接。

Is it true that if C is a static library, then the calls made to C from A and B each maintain a separate state of C ? 如果C是静态库,那么对AB进行的C调用分别保持C的独立状态,这是真的吗? Do they have different heaps? 他们有不同的堆吗?

On the other hand, if C is a shared library, then A and B would have access to a shared state of C ? 另一方面,如果C是共享库,则AB将有权访问C的共享状态。

ps the application entry point is in A or B . ps应用程序入口点在AB

It depends on your OS, and the options you use to compile and link. 这取决于您的操作系统以及用于编译和链接的选项。 Under Unix, by default, all common symbols in two or more shared libraries will resolve to the names in the first library loaded; 在Unix下,默认情况下,两个或多个共享库中的所有公共符号都将解析为第一个加载的库中的名称。 for most Unices, this applies to names in the main as well (but the GNU linker used in Linux requires a special option for this). 对于大多数Unices来说,这也适用于主名称(但是Linux中使用的GNU链接器为此需要一个特殊的选项)。 Under Windows, it's a bit trickier; 在Windows下,这比较麻烦。 each statically linked library will have its own copy of the state, and there's no easy work-around, other than creating a third DLL to wrap the static library, and only accessing it through symbols in that DLL. 每个静态链接库都将拥有自己的状态副本,除了创建第三个DLL来包装静态库并仅通过该DLL中的符号进行访问之外,没有简单的解决方法。

Static linking versus dynamic linking does not make a difference to the state management of the linked library from the point of view of your application. 从应用程序的角度来看,静态链接与动态链接对链接库的状态管理没有影响。 The library has a single state and all dependent modules are subject to it. 该库具有单个状态,所有从属模块都受其约束。

Where I think you are getting confused is in the optimizations that static linking versus dynamic linking allow for the operating system. 我认为您会感到困惑的是针对操作系统的静态链接与动态链接的优化。 A dynamic library can be loaded into memory once and mapped differently for each process that uses it. 动态库可以一次加载到内存中,并针对使用它的每个进程进行不同的映射。 This eliminates the need to load in exact copies of pages that are already loaded. 这样就无需加载已加载页面的精确副本。

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

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