分发Windows C ++库:如何决定创建静态库还是动态库? [英] Distributing Windows C++ library: how to decide whether to create static or dynamic library?

查看:50
本文介绍了分发Windows C ++库:如何决定创建静态库还是动态库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们一直在将Java和.NET API库转换为C ++,并正在尝试找出将编译版本分发给其他开发人员以与他们的自定义应用程序一起使用的最佳方法.它应该是静态库还是动态库?

We have been converting our Java and .NET API library to C++, and are trying to figure out what is the best way to distribute a compiled version to other developers to use with their custom applications. Should it be a static library or dynamic library?

我们需要同时为Win32和Win64创建(我想每个目标OS的Debug和Release版本).考虑到我在尝试确保所有引用的库都匹配时遇到的所有挫折感(/MT与/MD),我想知道是否有一个决策可以简化其他开发人员的工作.

We need to create for both Win32 and Win64 (and I suppose both a Debug and Release version of each target OS). Given all the frustration I've encountered trying to make sure all referenced libraries are matched (/MT versus /MD), I'm wondering if there is a decision to make here which will simplify it for other developers.

当我运行 dumpbin/all<静态库文件名>|在静态库上找到/i"msvc ,我看不到任何运行时引用(与在.exe或.dll上执行的引用不同).这表明运行时尚未链接,并为开发人员在开发和构建自己的应用程序时提供更大的灵活性来制作/MT或/MD?

When I run dumpbin /all <static library file name> | find /i "msvc on a static library, I don't see any runtime reference (unlike when I do the same on a .exe or .dll). Does that indicate that the runtime isn't linked yet, and that gives developer more flexibility to make the /MT or /MD when they develop and build their own app?

哪种方法会使开发人员的生活更轻松?

Which approach would make the developers' life easier?

推荐答案

静态和动态(共享)库都具有显着的优势.也许您的一种选择是分发两种类型的库,然后让库用户决定要使用哪种库.

There are significant advantages to both static and dynamic (shared) libraries. Maybe one option for you is to distribute both types of libraries, and let the library users decide which to use.

对于我自己,我通常使用静态库,除非我认为动态库的优点对项目有意义.

For myself, I usually use static libraries unless I think the advantages of a dynamic library are meaningful for the project.

静态库的优点:

  • 一旦由库的用户在编译时链接了,库中的代码始终位于需要调用它的同一模块中.因此,没有DLL的地狱,也没有并行的地狱.我无法计算尝试在Windows上运行某个程序以使其失败的次数,因为我没有安装正确版本的MSVC运行时(DLL).这确实是一种痛苦,如果可以避免,它将使每个人的生活更加轻松.
  • 类似地,库中代码的符号将最终出现在调用该库的模块的.pdb中,而不是您必须跟踪的另一个.pdb(.dll的.pdb)中.,到处复制等等.
  • 这是次要的,但是对于静态库,仅您需要链接的功能/数据最终出现在可执行文件中.而对于DLL,则是整个辣酱玉米饼馅.

动态库的优点

  • 明显的优点是,它允许在运行时甚至最终用户替换库,而无需重新链接.
  • 在大多数环境中,这是次要的事情,但是如果有很多可执行文件链接到该库中,则由于没有在每个可执行文件中重复相同的数据/代码,因此拥有DLL意味着磁盘空间更少.
  • 并非总是欣赏的东西: if 该库将同时由多个进程加载,如果将其作为DLL提供,则意味着-理想情况下-读取的文件只有一个副本,只需将数据(甚至可写数据,直到(除非由特定进程将其写入),否则可写数据)才需要存储在内存中.运行时链接DLL的所有进程共享内存中的相同字节.共享的内存是 总虚拟内存(RAM +页面文件)和 物理内存.但是,这只是最好的情况-如果无法在两个进程中将DLL加载到相同的虚拟地址,则它们将无法共享它.
  • The obvious advantage is that it allows the library to be replaced at runtime, even by an end-user, without a relink.
  • This is minor in most environments, but if a lot of executables link the library in, having a DLL means less disk space since the same data/code isn't repeated in every executable.
  • Something that isn't always appreciated: if the library is going to be loaded by multiple processes at the same time, providing it as a DLL means - ideally - only one copy of the read-only data (and even the writable data until and unless it is written to by a particular process) needs to be in memory. All the processes that runtime-link the DLL share the same bytes in memory. The memory shared is both total virtual memory (RAM+pagefile) and physical memory. However this is all only best-case - if the DLL can't be loaded at the same virtual address in two processes, they can't share it.

这篇关于分发Windows C ++库:如何决定创建静态库还是动态库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆