静态库和共享库的区别?

时间:2022-11-19
本文介绍了静态库和共享库的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

静态库和共享库有什么区别?

What is the difference between static and shared libraries?

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

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

推荐答案

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

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.

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

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.

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

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.

这篇关于静态库和共享库的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何在 C 预处理器中可靠地检测 Mac OS X、iOS、L 下一篇:为面向对象的 C++ 代码开发 C 包装器 API

相关文章

最新文章