简体   繁体   English

静态库和共享库的区别?

[英]Difference between static and shared libraries?

What is the difference between static and shared libraries?静态库和共享库有什么区别?

I use Eclipse and there are several project types including Static Libraries and Shared Libraries?我使用 Eclipse 并且有几种项目类型,包括静态库和共享库? Does one have an advantage over the other?一个比另一个有优势吗?

Shared libraries are .so (or in Windows .dll, or in OS X .dylib) files.共享库是 .so(或在 Windows .dll 中,或在 OS X .dylib 中)文件。 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.静态库是 .a(或在 Windows 中的 .lib)文件。 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]. [Windows 也有用于引用 .dll 文件的 .lib 文件,但它们的作用与第一个相同]。

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.就个人而言,我更喜欢共享库,但在需要确保二进制文件没有许多可能难以满足的外部依赖项时使用静态库,例如特定版本的 C++ 标准库或特定版本的 Boost C++ 库。

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.使用静态库,书籍/功能是你的,你把它保存在你的家/程序中,一旦你有了它,你就不会关心在哪里或何时得到它。

Simplified:简化:

  • Static linking: one large executable静态链接:一个大型可执行文件
  • Dynamic linking: a small executable plus one or more library files (.dll files on Windows, .so on Linux, or .dylib on macOS)动态链接:一个小的可执行文件加上一个或多个库文件(Windows 上的 .dll 文件,Linux 上的 .so 或 macOS 上的 .dylib)

For a static library, the code is extracted from the library by the linker and used to build the the final executable at the point you compile/build your application.对于静态库,链接器从库中提取代码,并在您编译/构建应用程序时用于构建最终的可执行文件。 The final executable has no dependencies on the library at run time最终的可执行文件在运行时不依赖于库

For a shared library, the compiler/linker checks that the names you link with exist in the library when the application is built, but doesn't move their code into the application.对于共享库,编译器/链接器会在构建应用程序时检查您链接的名称是否存在于库中,但不会将它们的代码移动到应用程序中。 At run time, the shared library must be available.在运行时,共享库必须可用。

The C programming language itself has no concept of either static or shared libraries - they are completely an implementation feature. C 编程语言本身没有静态库或共享库的概念——它们完全是一个实现特性。

Personally, I much prefer to use static libraries, as it makes software distribution simpler.就个人而言,我更喜欢使用静态库,因为它使软件分发更简单。 However, this is an opinion over which much (figurative) blood has been shed in the past.然而,这是关于过去流过多少(比喻性的)血的观点。

Static libraries are compiled as part of an application, whereas shared libraries are not.静态库作为应用程序的一部分进行编译,而共享库则不是。 When you distribute an application that depends on shared libaries, the libraries, eg.当您分发依赖于共享库的应用程序时,库,例如。 dll's on MS Windows need to be installed.需要安装 MS Windows 上的 dll。

The advantage of static libraries is that there are no dependencies required for the user running the application - eg they don't have to upgrade their DLL of whatever.静态库的优点是运行应用程序的用户不需要依赖项——例如,他们不必升级他们的 DLL。 The disadvantage is that your application is larger in size because you are shipping it with all the libraries it needs.缺点是您的应用程序体积较大,因为您将它与它需要的所有库一起发送。

As well as leading to smaller applications, shared libraries offer the user the ability to use their own, perhaps better version of the libraries rather than relying on one that's part of the application除了导致较小的应用程序,共享库还为用户提供了使用他们自己的库的能力,也许是更好的库版本,而不是依赖于应用程序的一部分

The most significant advantage of shared libraries is that there is only one copy of code loaded in memory, no matter how many processes are using the library.共享库最显着的优势在于,无论有多少进程正在使用该库,内存中都只加载了一份代码副本。 For static libraries each process gets its own copy of the code.对于静态库,每个进程都有自己的代码副本。 This can lead to significant memory wastage.这会导致显着的内存浪费。

OTOH, a advantage of static libraries is that everything is bundled into your application. OTOH,静态库的一个优点是一切都捆绑到您的应用程序中。 So you don't have to worry that the client will have the right library (and version) available on their system.因此,您不必担心客户端将在其系统上提供正确的库(和版本)。

On top of all the other answers, one thing not mentionned yet is decoupling :除了所有其他答案之外,还没有提到的一件事是解耦:

Let me speak about a real world production code,that I have been dealing with :让我谈谈我一直在处理的真实世界生产代码:

A very big software, made of >300 projects (with visual studio), mostly build as static lib and finally all link together in one huge executable , you end up with the following problems :一个非常大的软件,由超过 300 个项目(使用 Visual Studio)组成,主要构建为静态库,最后全部链接到一个巨大的可执行文件中,最终会遇到以下问题:

-Link time is extremely long. - 链接时间非常长。 You might end up by more than 15min of link, for let's say 10s of compilation time -Some tools are on their knee with such a big executable , like memory check tools that must instrument the code.您最终可能需要超过 15 分钟的链接,比如 10 秒的编译时间 - 有些工具面临如此大的可执行文件,例如必须检测代码的内存检查工具。 You might fall into reaching limits that had been seen as fools.您可能会陷入被视为傻瓜的极限。

More problematic is the decoupling of your software : on this real world example, headers files of every project were reacheable from any others projects.更大的问题是你的软件的解耦:在这个真实世界的例子中,每个项目的头文件都可以从任何其他项目中访问。 As a consequence it was extremely easy for one developer to add dependencies;因此,一名开发人员非常容易添加依赖项; it was just about including the header, because link at the end will allwaws find symbols.它只是包含标题,因为最后的链接将始终可以找到符号。 It ends up by horrible cycling dependencies and complete mess.它以可怕的循环依赖和完全混乱而告终。

With shared library, it's a bit of extra work because developer must edit the project build system to add the dependent library.使用共享库,这是一些额外的工作,因为开发人员必须编辑项目构建系统以添加依赖库。 I observed that shared library code tends to offer a cleaner code API.我观察到共享库代码倾向于提供更清晰的代码 API。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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