visual-studio 常规 DLL 使用:MFC 共享与 MFC 静态链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2652679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular DLL using: MFC Shared vs MFC statically linked
提问by AKN
When we create a DLL using Visual studio (VC8 or 9), we get an option as create Regular DLL
当我们使用 Visual Studio(VC8 或 9)创建 DLL 时,我们会得到一个选项,即 create Regular DLL
using MFC as shared DLL
or
或者
using MFC as static library
How are they different? Which one is advisable to use?
它们有何不同?建议使用哪一种?
采纳答案by AKN
[I think I got my answer now]
[我想我现在得到了答案]
If you use MFC DLL as dynamic linking, your code will need the Microsoft Foundation Library DLL's (specifically the version your code requires) installed along with your application or dll in the user end. So this means your installation package would contain
如果您使用 MFC DLL 作为动态链接,您的代码将需要在用户端与您的应用程序或 dll 一起安装 Microsoft Foundation Library DLL(特别是您的代码所需的版本)。所以这意味着你的安装包将包含
- Your application/DLL and supporting files
- All MFC Dlls
- 您的应用程序/DLL 和支持文件
- 所有 MFC DLL
This makes the installation package size go bigger and also make take time for user to download your installation setup.
这使得安装包的大小变得更大,也使用户需要时间来下载您的安装设置。
If you link to MFC as static library, you code will work even without MFC DLLs present at the user end . The reason being pretty simple that all the MFC libraries you refererred in your code, will be linked into your application or dll. This means those MFC libraries used in your app/dll becomes the part of the your binary; however, your app/dll will be little bigger.
如果您将 MFC 作为静态库链接,即使用户端没有 MFC DLL,您的代码也能正常工作。原因很简单,您在代码中引用的所有 MFC 库都将链接到您的应用程序或 dll。这意味着在您的应用程序/dll 中使用的那些 MFC 库将成为您的二进制文件的一部分;但是,您的应用程序/dll 会大一点。
回答by Andy Shellam
A static library means the code you use from the library is included in your executable. Because of this, you don't need to ship the library or require the end user to have it on their machine. However this bloats the size of your executable and ties you to that library version, so if you need to update just the library, you have to ship a new executable.
静态库意味着您从库中使用的代码包含在您的可执行文件中。因此,您不需要运送库或要求最终用户在他们的机器上安装它。但是,这会增加可执行文件的大小并将您绑定到该库版本,因此如果您只需要更新库,则必须发布新的可执行文件。
A shared library calls the library at the time it needs it (runtime) to execute the code, but it requires the user to have it (usually a specific or minimum version) installed on their machine. You can also distribute the required version of the library with your application if you need to.
共享库在需要时(运行时)调用库来执行代码,但它要求用户在他们的机器上安装它(通常是特定或最低版本)。如果需要,您还可以随应用程序分发所需版本的库。
As for which is better, I don't know. I'm not a Windows C++ or MFC programmer so I couldn't say. On my Linux servers, the applications I write are generally server-side and thus use shared libraries.
至于哪个更好,我不知道。我不是 Windows C++ 或 MFC 程序员,所以我不能说。在我的 Linux 服务器上,我编写的应用程序通常是服务器端的,因此使用共享库。
It depends on how your application is to be used, distributed, updated, how often the MFC library changes, if it's generally available on user's PCs etc.
这取决于您的应用程序的使用、分发、更新方式,MFC 库更改的频率,是否在用户的 PC 上普遍可用等。
回答by Woodturner
Another consideration is servicing your application.
另一个考虑因素是为您的应用程序提供服务。
If you ship the MSFT redis, dynamically linking against its libraries, and then MSFT later "fixes" some vital flaw in a DLL, they patch the DLL on your customer's machines through Window's Update. If you statically link, you will need to update all your customers directly.
如果您发布 MSFT redis,动态链接其库,然后 MSFT 稍后“修复”DLL 中的一些重要缺陷,他们会通过 Window 的更新修补您客户机器上的 DLL。如果您静态链接,则需要直接更新所有客户。
Of course, if you are concerned that a patched DLL might break your application (because you rely on unspecified behavior), you may want to handle the servicing (and testing) directly with your customer.
当然,如果您担心修补后的 DLL 可能会破坏您的应用程序(因为您依赖于未指定的行为),您可能希望直接与您的客户一起处理服务(和测试)。

