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

时间:2022-01-16 02:51:00

What is the difference between static and shared libraries?

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

I use Eclipse and there are several project types including Static Libraries and Shared Libraries? Does one have an advantage over the other?

我使用Eclipse,还有一些项目类型,包括静态库和共享库?一个人比另一个人有优势吗?

7 个解决方案

#1


609  

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.

共享库是so(或在Windows .dll中,或在OS X .dylib)文件中。与库相关的所有代码都在这个文件中,并且它是在运行时使用它的程序引用的。使用共享库的程序只会引用它在共享库中使用的代码。

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].

静态库是一个(或在Windows .lib)文件中。所有与库相关的代码都在这个文件中,并且它在编译时直接链接到程序中。使用静态库的程序从静态库中获取它使用的代码的副本,并使其成为程序的一部分。[Windows也有。lib文件,用于引用.dll文件,但它们的作用方式与第一个文件相同。

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++库的特定版本。

#2


328  

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.

静态库就像书店,共享的库就像…一个图书馆。有了前者,你就可以得到你自己的书/功能,带回家;有了后者,你和其他人都去图书馆使用同样的书/功能。因此,任何想使用(共享)库的人都需要知道它在哪里,因为您必须“去获取”图书/函数。有了一个静态的库,书/函数是你自己的,你把它保存在你的家里/程序里,一旦你有了它,你就不在乎你在哪里或什么时候得到它。

#3


47  

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上,或者在macOS上的。dylib

#4


28  

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.

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

The advantages of static libraries is that there are no dependancies required for the user running the application - e.g. they don't have to upgrade their DLL of whatever... The disadvantages is that your application is larger in size because you are shipping it with all the libraries it needs.

静态库的优点是,运行应用程序的用户不需要依赖性——例如,他们不需要升级他们的DLL。缺点是您的应用程序的规模更大,因为您将它与它需要的所有库一起发送。

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

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

#5


25  

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.

就我个人而言,我更喜欢使用静态库,因为它使软件发布更加简单。然而,这是一种观点,在过去,许多(比喻性的)血液已被抛弃。

#6


14  

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. So you don't have to worry that the client will have the right library (and version) available on their system.

OTOH,静态库的优点是,所有的东西都被绑定到您的应用程序中。因此,您不必担心客户机将在其系统上拥有正确的库(和版本)。

#7


2  

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. You might fall into reaching limits that had been seen as fools.

链接时间非常长。您可能会以超过15min的链接结束,比方说10秒的编译时间——有些工具在他们的膝盖上有这样一个大的可执行文件,就像内存检查工具一样,必须对代码进行测试。你可能会陷入被视为傻瓜的极限。

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。

#1


609  

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.

共享库是so(或在Windows .dll中,或在OS X .dylib)文件中。与库相关的所有代码都在这个文件中,并且它是在运行时使用它的程序引用的。使用共享库的程序只会引用它在共享库中使用的代码。

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].

静态库是一个(或在Windows .lib)文件中。所有与库相关的代码都在这个文件中,并且它在编译时直接链接到程序中。使用静态库的程序从静态库中获取它使用的代码的副本,并使其成为程序的一部分。[Windows也有。lib文件,用于引用.dll文件,但它们的作用方式与第一个文件相同。

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++库的特定版本。

#2


328  

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.

静态库就像书店,共享的库就像…一个图书馆。有了前者,你就可以得到你自己的书/功能,带回家;有了后者,你和其他人都去图书馆使用同样的书/功能。因此,任何想使用(共享)库的人都需要知道它在哪里,因为您必须“去获取”图书/函数。有了一个静态的库,书/函数是你自己的,你把它保存在你的家里/程序里,一旦你有了它,你就不在乎你在哪里或什么时候得到它。

#3


47  

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上,或者在macOS上的。dylib

#4


28  

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.

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

The advantages of static libraries is that there are no dependancies required for the user running the application - e.g. they don't have to upgrade their DLL of whatever... The disadvantages is that your application is larger in size because you are shipping it with all the libraries it needs.

静态库的优点是,运行应用程序的用户不需要依赖性——例如,他们不需要升级他们的DLL。缺点是您的应用程序的规模更大,因为您将它与它需要的所有库一起发送。

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

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

#5


25  

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.

就我个人而言,我更喜欢使用静态库,因为它使软件发布更加简单。然而,这是一种观点,在过去,许多(比喻性的)血液已被抛弃。

#6


14  

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. So you don't have to worry that the client will have the right library (and version) available on their system.

OTOH,静态库的优点是,所有的东西都被绑定到您的应用程序中。因此,您不必担心客户机将在其系统上拥有正确的库(和版本)。

#7


2  

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. You might fall into reaching limits that had been seen as fools.

链接时间非常长。您可能会以超过15min的链接结束,比方说10秒的编译时间——有些工具在他们的膝盖上有这样一个大的可执行文件,就像内存检查工具一样,必须对代码进行测试。你可能会陷入被视为傻瓜的极限。

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。