問(wèn)題描述
靜態(tài)庫(kù)和共享庫(kù)有什么區(qū)別?
What is the difference between static and shared libraries?
我使用 Eclipse 并且有幾種項(xiàng)目類型,包括靜態(tài)庫(kù)和共享庫(kù)?一個(gè)比另一個(gè)有優(yōu)勢(shì)嗎?
I use Eclipse and there are several project types including Static Libraries and Shared Libraries? Does one have an advantage over the other?
推薦答案
共享庫(kù)是 .so(或在 Windows .dll 中,或在 OS X .dylib 中)文件.與庫(kù)相關(guān)的所有代碼都在這個(gè)文件中,并且在運(yùn)行時(shí)被使用它的程序引用.使用共享庫(kù)的程序只引用它在共享庫(kù)中使用的代碼.
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.
靜態(tài)庫(kù)是 .a(或在 Windows 中的 .lib)文件.與庫(kù)相關(guān)的所有代碼都在這個(gè)文件中,并且在編譯時(shí)直接鏈接到程序中.使用靜態(tài)庫(kù)的程序從靜態(tài)庫(kù)中復(fù)制它使用的代碼,并使其成為程序的一部分.[Windows 也有用于引用 .dll 文件的 .lib 文件,但它們的作用與第一個(gè)相同].
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].
每種方法都有優(yōu)點(diǎn)和缺點(diǎn):
There are advantages and disadvantages in each method:
共享庫(kù)減少了使用該庫(kù)的每個(gè)程序中重復(fù)的代碼量,從而使二進(jìn)制文件保持較小.它還允許您用功能等效的共享對(duì)象替換共享對(duì)象,但可能會(huì)增加性能優(yōu)勢(shì),而無(wú)需重新編譯使用它的程序.但是,共享庫(kù)會(huì)產(chǎn)生少量額外的函數(shù)執(zhí)行成本以及運(yùn)行時(shí)加載成本,因?yàn)閹?kù)中的所有符號(hào)都需要連接到它們使用的東西.此外,共享庫(kù)可以在運(yùn)行時(shí)加載到應(yīng)用程序中,這是實(shí)現(xiàn)二進(jìn)制插件系統(tǒng)的通用機(jī)制.
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.
靜態(tài)庫(kù)會(huì)增加二進(jìn)制文件的整體大小,但這意味著您無(wú)需攜帶正在使用的庫(kù)的副本.由于代碼是在編譯時(shí)連接的,因此沒(méi)有任何額外的運(yùn)行時(shí)加載成本.代碼就在那里.
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.
就個(gè)人而言,我更喜歡共享庫(kù),但在需要確保二進(jìn)制文件沒(méi)有很多可能難以滿足的外部依賴項(xiàng)時(shí)使用靜態(tài)庫(kù),例如特定版本的 C++ 標(biāo)準(zhǔn)庫(kù)或特定版本的 Boost C++圖書(shū)館.
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.
這篇關(guān)于靜態(tài)庫(kù)和共享庫(kù)的區(qū)別?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!