在c++中创建类库时,可以在动态(.dll, .so)和静态(.dll, .so)之间进行选择。Lib, .a)库。它们之间的区别是什么?什么时候使用哪个比较合适?
当前回答
静态库必须链接到最终的可执行文件中;它成为可执行文件的一部分,并跟随它到任何地方。每次执行可执行文件时都会加载动态库,并以DLL文件的形式与可执行文件分开。
当您希望能够更改库提供的功能而不必重新链接可执行文件(只需替换DLL文件,而不必替换可执行文件)时,您将使用DLL。
当您没有理由使用动态库时,您可以使用静态库。
其他回答
静态库必须链接到最终的可执行文件中;它成为可执行文件的一部分,并跟随它到任何地方。每次执行可执行文件时都会加载动态库,并以DLL文件的形式与可执行文件分开。
当您希望能够更改库提供的功能而不必重新链接可执行文件(只需替换DLL文件,而不必替换可执行文件)时,您将使用DLL。
当您没有理由使用动态库时,您可以使用静态库。
您应该仔细考虑随时间的变化、版本控制、稳定性、兼容性等。
如果有两个应用程序使用共享代码,您是否希望强制这些应用程序一起更改,以防它们需要相互兼容?然后使用dll。所有的exe都将使用相同的代码。
或者你想把它们彼此隔离,这样你就可以改变一个,并确信你没有破坏另一个。然后使用静态库。
DLL地狱是当你可能应该使用一个静态库,但你使用了一个DLL代替,并不是所有的前任都与它兼容。
如果库是静态的,则在链接时将代码链接到可执行文件中。这使您的可执行文件更大(如果您走动态路线)。
如果库是动态的,那么在链接时,对所需方法的引用将内置于可执行文件中。这意味着您必须发布可执行文件和动态库。您还应该考虑对库中代码的共享访问是否安全、首选加载地址以及其他事项。
如果你能接受静态库,那就使用静态库。
静态库增加了二进制文件中代码的大小。它们总是被加载,无论你用什么版本的代码编译,都是将运行的代码版本。
动态库的存储和版本是分开的。如果更新被认为与原始版本二进制兼容,则有可能加载的动态库版本不是随代码附带的原始版本。
此外,动态库不一定被加载——它们通常在第一次调用时被加载——并且可以在使用相同库的组件之间共享(多个数据加载,一个代码加载)。
动态库在大多数时候被认为是更好的方法,但最初它们有一个重大缺陷(谷歌DLL地狱),这个缺陷几乎被最近的Windows操作系统(特别是Windows XP)所消除。
其他人已经充分解释了静态库是什么,但我想指出一些使用静态库的注意事项,至少在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.
推荐文章
- 什么是“参数依赖查找”(又名ADL,或“Koenig查找”)?
- 公共朋友交换成员函数
- 如何在Go中使用c++
- 自定义c++分配器的引人注目的例子?
- RAII和c++中的智能指针
- 如何构建和使用谷歌TensorFlow c++ api
- 断言是邪恶的吗?
- 下面这些短语在c++中是什么意思:0 -,default-和value-initialization?
- 在STL地图中,使用map::insert比[]更好吗?
- C++ Linux的想法?
- 如何为Fedora安装g++ ?
- Std::cin输入空格?
- c++标准是否要求iostreams的性能很差,或者我只是在处理一个糟糕的实现?
- gcc在哪里查找C和c++头文件?
- 为什么我们需要require require ?