shared lib and static lib

Shared libraries are .so (or in Windows .dll, or in OS X .dylib) files. All the code relating to the library is in this file, and it is referenced by programs using it at run-time. A program using a shared library only makes reference to the code that it uses in the shared library.

Static libraries are .a (or in Windows .lib) files. All the code relating to the library is in this file, and it is directly linked into the program at compile time. A program using a static library takes copies of the code that it uses from the static library and makes it part of the program. [Windows also has .lib files which are used to reference .dll files, but they act the same way as the first one].

There are advantages and disadvantages in each method:

Shared libraries reduce the amount of code that is duplicated in each program that makes use of the library, keeping the binaries small. It also allows you to replace the shared object with one that is functionally equivalent, but may have added performance benefits without needing to recompile the program that makes use of it. Shared libraries will, however have a small additional cost for the execution of the functions as well as a run-time loading cost as all the symbols in the library need to be connected to the things they use. Additionally, shared libraries can be loaded into an application at run-time, which is the general mechanism for implementing binary plug-in systems.

Static libraries increase the overall size of the binary, but it means that you don't need to carry along a copy of the library that is being used. As the code is connected at compile time there are not any additional run-time loading costs. The code is simply there.

Personally, I prefer shared libraries, but use static libraries when needing to ensure that the binary does not have many external dependencies that may be difficult to meet, such as specific versions of the C++ standard library or specific versions of the Boost C++ library.

(1) .dll .obj .lib使用在windows平台下。
.dll:动态链接库,作为共享函数库的可执行文件。
.obj:目标文件,相当于源代码对应的二进制文件,未经重定位。
.lib:可理解为多个obj的集合,本质与.obj相同。

(2) .so .o .a使用在linux平台下。
.so:(share object)动态链接库,跟Windows平台类似。
.o: 目标文件,相当于源代码对应的二进制文件。
.a: 与.o类似,多个.o的集合。

posted @ 2021-07-27 14:36  千心  阅读(141)  评论(0编辑  收藏  举报