Static Vs Dynamic libraries

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 09:09:36

问题


I have read about static and dynamic libraries. My question is little specifie

dlopen dlclose :

Benifit of dlopen is  we can start the EXE with out
loading the necessary libraries at the begining. Only when we
need we will load the libratries and unload it from the memory.

This is behaviour of dynamic linking libaries.

My question is if i link the library libUtlities

ld -o EXE main.o -lUtilities 

When I run the EXE, libUtlities will be loaded to the memory before I first use there functionalities

which i observed in dbx (Solaris debugger)
But will not contribute to the size of the EXE.

1.So is it a static linking or dynamic linking. ?


回答1:


Unfortunately, the words "static" and "dynamic" are way too overused, especially in C and C++. So, I prefer the following terminology:

  • Link-time linking, a.k.a "static linking": All symbols are resolved at link time from static libraries. The result is a monolithic, statically linked executable with no load-time dependencies.

  • Load-time linking: This is the standard practice on modern platforms, unresolved symbols are looked up in shared libraries (Unix) or the unfortunately named dynamic link libraries (DLLS) on Windows and only references are recorded at link time, the actual resolution of the symbols and code loading happens at load time.

    This results in a "dynamically linked" executable which must be loaded with a loader (e.g. ld.so on Linux). Loading is part of the OS and usually transparent to the user, though it's open to inspection (e.g. with ldd on Linux). All shared libraries must be available at load time, or the program will not launch.

  • Run-time linking, a.k.a. "dynamic linking": There are no unresolved symbols; rather, the runtime dynamically decides to look up symbols in a shared/dynamic library using dlopen() or LoadLibrary(). Failure to find symbols is a handlable runtime condition that is not an error. This technique is used commonly for plug-in architecture, and on Windows for code injection.

Note however that there is a fundamental technical difference between Linux's shared objects and Windows's DLLs, they're not just the same thing with a different name. Both can however be used both for load-time and run-time linking.




回答2:


It is dynamic linking. It has nothing to do with dlopen dlclose. By dlopen you are manually opening dynamic library and calling functions exported from it. By dynamic linking all this job is done by linker. Static linking is linking against static library (.a file). By static linking the code from library is linked into your exe increasing it's size.



来源:https://stackoverflow.com/questions/7214764/static-vs-dynamic-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!