简体   繁体   中英

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 .

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 ? 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 ?

ps the application entry point is in A or B .

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; 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). Under Windows, it's a bit trickier; 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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