在c++中创建类库时,可以在动态(.dll, .so)和静态(.dll, .so)之间进行选择。Lib, .a)库。它们之间的区别是什么?什么时候使用哪个比较合适?
当前回答
Static libraries are archives that contain the object code for the library, when linked into an application that code is compiled into the executable. Shared libraries are different in that they aren't compiled into the executable. Instead the dynamic linker searches some directories looking for the library(s) it needs, then loads that into memory. More then one executable can use the same shared library at the same time, thus reducing memory usage and executable size. However, there are then more files to distribute with the executable. You need to make sure that the library is installed onto the uses system somewhere where the linker can find it, static linking eliminates this problem but results in a larger executable file.
其他回答
其他人已经充分解释了静态库是什么,但我想指出一些使用静态库的注意事项,至少在Windows上:
Singletons: If something needs to be global/static and unique, be very careful about putting it in a static library. If multiple DLLs are linked against that static library they will each get their own copy of the singleton. However, if your application is a single EXE with no custom DLLs, this may not be a problem. Unreferenced code removal: When you link against a static library, only the parts of the static library that are referenced by your DLL/EXE will get linked into your DLL/EXE. For example, if mylib.lib contains a.obj and b.obj and your DLL/EXE only references functions or variables from a.obj, the entirety of b.obj will get discarded by the linker. If b.obj contains global/static objects, their constructors and destructors will not get executed. If those constructors/destructors have side effects, you may be disappointed by their absence. Likewise, if the static library contains special entrypoints you may need to take care that they are actually included. An example of this in embedded programming (okay, not Windows) would be an interrupt handler that is marked as being at a specific address. You also need to mark the interrupt handler as an entrypoint to make sure it doesn't get discarded. Another consequence of this is that a static library may contain object files that are completely unusable due to unresolved references, but it won't cause a linker error until you reference a function or variable from those object files. This may happen long after the library is written. Debug symbols: You may want a separate PDB for each static library, or you may want the debug symbols to be placed in the object files so that they get rolled into the PDB for the DLL/EXE. The Visual C++ documentation explains the necessary options. RTTI: You may end up with multiple type_info objects for the same class if you link a single static library into multiple DLLs. If your program assumes that type_info is "singleton" data and uses &typeid() or type_info::before(), you may get undesirable and surprising results.
c++程序的构建分两个阶段
编译——生成目标代码(.obj) 链接——生成可执行代码(.exe或.dll)
静态库(.lib)只是一个.obj文件的包,因此不是一个完整的程序。它还没有经历构建程序的第二个(链接)阶段。另一方面,dll类似于exe,因此是完整的程序。
如果你构建了一个静态库,它还没有被链接,因此你的静态库的消费者将不得不使用与你使用的相同的编译器(如果你使用g++,他们将不得不使用g++)。
如果相反,您构建了一个dll(并且正确地构建了它),那么您已经构建了一个所有消费者都可以使用的完整程序,无论他们使用哪种编译器。但是,如果需要跨编译器兼容性,则从dll导出有几个限制。
如果库是静态的,则在链接时将代码链接到可执行文件中。这使您的可执行文件更大(如果您走动态路线)。
如果库是动态的,那么在链接时,对所需方法的引用将内置于可执行文件中。这意味着您必须发布可执行文件和动态库。您还应该考虑对库中代码的共享访问是否安全、首选加载地址以及其他事项。
如果你能接受静态库,那就使用静态库。
库是绑定在应用程序可执行文件中的代码单元。
dll是可执行代码的独立单元。只有在对该代码进行调用时,才会在流程中加载它。一个dll可以被多个应用程序使用并加载在多个进程中,而在硬盘驱动器上仍然只有一个代码副本。
Dll优点:可用于多个产品之间重用/共享代码;按需装入进程内存,不需要时可卸载;可以独立于程序的其余部分进行升级。
Dll缺点:Dll加载和代码重基的性能影响;版本控制问题(“dll地狱”)
Lib优点:没有性能影响,因为代码总是在进程中加载,而不是重基;没有版本问题。
Lib缺点:可执行文件/进程“膨胀”-所有的代码都在你的可执行文件中,并在进程启动时加载;没有重用/共享-每个产品都有自己的代码副本。
关于这个主题的精彩讨论,请阅读Sun的这篇文章。
它包含了所有的好处,包括能够插入插入库。关于插入的更多细节可以在这篇文章中找到。
推荐文章
- 如何构建和使用谷歌TensorFlow c++ api
- 断言是邪恶的吗?
- 下面这些短语在c++中是什么意思:0 -,default-和value-initialization?
- 在STL地图中,使用map::insert比[]更好吗?
- C++ Linux的想法?
- 如何为Fedora安装g++ ?
- Std::cin输入空格?
- c++标准是否要求iostreams的性能很差,或者我只是在处理一个糟糕的实现?
- gcc在哪里查找C和c++头文件?
- 为什么我们需要require require ?
- 解析c++中的命令行参数?
- 我如何在c++中创建一个随机的字母数字字符串?
- c++中的atan和atan2有什么区别?
- 现代c++能让你免费获得性能吗?
- 一门语言是如何自我扩展的?