静态库和共享库之间的区别是什么?

我使用Eclipse,有几个项目类型,包括静态库和共享库?其中一种比另一种有优势吗?


当前回答

对于静态库,代码由链接器从库中提取,并在编译/构建应用程序时用于构建最终的可执行文件。最终的可执行文件在运行时不依赖于库

对于共享库,编译器/链接器在构建应用程序时检查所链接的名称是否存在于库中,但不会将它们的代码移到应用程序中。在运行时,共享库必须可用。

C编程语言本身没有静态库或共享库的概念——它们完全是一种实现特性。

就我个人而言,我更喜欢使用静态库,因为它使软件分发更简单。然而,这一观点在过去曾引发大量(象征性的)流血冲突。

其他回答

静态库是作为应用程序的一部分编译的,而共享库不是。当您分发依赖于共享库的应用程序时,库,例如。需要安装MS Windows上的dll文件。

静态库的优点是用户运行应用程序时不需要依赖任何东西——例如,他们不需要升级他们的DLL。缺点是您的应用程序的大小更大,因为您附带了它所需的所有库。

除了导致更小的应用程序之外,共享库还为用户提供了使用他们自己的,也许是更好版本的库的能力,而不是依赖于应用程序的一部分

-------------------------------------------------------------------------
|  +-  |    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文件)

在所有其他答案中,有一件事还没有被提及,那就是脱钩:

让我来谈谈我一直在处理的一个真实世界的生产代码:

一个非常大的软件,由>300个项目(visual studio)组成,主要构建为静态库,最后所有链接在一个巨大的可执行文件中,你最终会遇到以下问题:

-链接时间过长。你可能会有超过15分钟的链接,比如10秒的编译时间 -有些工具在处理这么大的可执行文件时是非常困难的,比如内存检查工具,它们必须检测代码。你可能会陷入被视为傻瓜的极限。

更有问题的是软件的解耦:在这个真实的例子中,任何其他项目都可以访问每个项目的头文件。因此,对于一个开发人员来说,添加依赖项非常容易;它只是包括标题,因为链接在最后将允许waws找到符号。它以可怕的循环依赖和完全混乱而告终。

使用共享库,需要做一些额外的工作,因为开发人员必须编辑项目构建系统来添加依赖库。我发现共享库代码往往提供更简洁的代码API。

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