静态库和共享库之间的区别是什么?
我使用Eclipse,有几个项目类型,包括静态库和共享库?其中一种比另一种有优势吗?
静态库和共享库之间的区别是什么?
我使用Eclipse,有几个项目类型,包括静态库和共享库?其中一种比另一种有优势吗?
当前回答
共享库最显著的优点是,不管有多少进程在使用这个库,内存中只加载一份代码。对于静态库,每个进程都有自己的代码副本。这可能导致严重的内存浪费。
静态库的一个优点是所有东西都捆绑到应用程序中。因此,您不必担心客户端系统上是否有正确的库(和版本)可用。
其他回答
共享库最显著的优点是,不管有多少进程在使用这个库,内存中只加载一份代码。对于静态库,每个进程都有自己的代码副本。这可能导致严重的内存浪费。
静态库的一个优点是所有东西都捆绑到应用程序中。因此,您不必担心客户端系统上是否有正确的库(和版本)可用。
A static library is like a bookstore, and a shared library is like... a library. With the former, you get your own copy of the book/function to take home; with the latter you and everyone else go to the library to use the same book/function. So anyone who wants to use the (shared) library needs to know where it is, because you have to "go get" the book/function. With a static library, the book/function is yours to own, and you keep it within your home/program, and once you have it you don't care where or when you got it.
+---------------+---------------------------+------------------------------+
| properties | Static library | Shared library |
+===============+===========================+==============================+
| Linking time | It happens as the | Shared libraries |
| | last step of the | are added during |
| | compilation process. | linking process |
| | After the program | when executable |
| | is placed | file and libraries |
| | in the memory | are added to the memory. |
+---------------+---------------------------+------------------------------+
| Means | Performed by linkers | Performed by operating System|
+---------------+---------------------------+------------------------------+
| Size | Static libraries are | Dynamic libraries are |
| | much bigger in size, | much smaller, because |
| | because external | there is only one copy |
| | programs are built | of dynamic library |
| | in the executable file. | that is kept in memory. |
+---------------+---------------------------+------------------------------+
| External file | Executable file will | In shared libraries, |
| changes | have to be recompiled | no need to recompile |
| | if any changes were | the executable. |
| | applied to external files.| |
+---------------+---------------------------+------------------------------+
| Time | Takes longer to execute | It is faster |
| | because loading into the | because shared |
| | memory happens every time | library code is |
| | while executing. | already in the memory. |
+---------------+---------------------------+------------------------------+
| Compatibility | Never has a compatibility | Programs are dependent |
| | issue,since all code is | on having a compatible |
| | in one executable module. | library.Dependent program |
| | | will not work if library |
| | | gets removed from the system |
+---------------+---------------------------+------------------------------+
-------------------------------------------------------------------------
| +- | Shared(dynamic) | Static Library (Linkages) |
-------------------------------------------------------------------------
|Pros: | less memory use | an executable, using own libraries|
| | | ,coming with the program, |
| | | doesn't need to worry about its |
| | | compilebility subject to libraries|
-------------------------------------------------------------------------
|Cons: | implementations of | bigger memory uses |
| | libraries may be altered | |
| | subject to OS and its | |
| | version, which may affect| |
| | the compilebility and | |
| | runnability of the code | |
-------------------------------------------------------------------------
简化:
静态链接:一个大型可执行文件 动态链接:一个小的可执行文件加上一个或多个库文件(Windows上是.dll文件,Linux上是.so文件,macOS上是.dylib文件)