我应该静态或动态链接到Visual Studio C运行时? [英] Should I link to the Visual Studio C runtime statically or dynamically?

查看:123
本文介绍了我应该静态或动态链接到Visual Studio C运行时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关在Visual Studio项目中是静态链接还是动态链接到C运行时库的两面论据,但我仍然不确定要怎么想。

I have read arguments on both sides about whether one should link to the C runtime library statically or dynamically in Visual Studio projects, and I'm still not entirely sure what to think.

我的项目引入了一些第三方库(Python,HDF5,Trilinos和Microsoft MPI),每个库都必须使用与最终可执行文件相同的运行时库进行构建(否则它们无法链接在一起) 。静态链接时,每个库都将包含C运行时的副本。我读到这很容易引起问题,因为最终的可执行文件将包含运行时的多个副本,而这些副本都无法彼此交互。但是链接器是否会抱怨是否重复定义了相同的符号?

My project pulls in some third-party libraries (Python, HDF5, Trilinos, and Microsoft MPI), each of which has to be built with the same runtime library as my final executable (otherwise they cannot be linked together). When linking statically, each of these libraries will contain a copy of the C runtime. I read that this is liable to cause issues because the final executable will contain multiple copies of the runtime, none of which can interact with each other. But wouldn't the linker complain if the same symbols were multiply defined?

我想避免出现 DLL Hell,但担心静态产生的隐式错误链接运行时的多个副本。我读错了吗?

I would like to avoid "DLL Hell" but am worried about insidious errors that could arise from statically linking in multiple copies of the runtime. Am I reading things wrong?

此外,我使用的是Visual Studio 2005,但我读到Service Pack 1运行时不向后兼容。这是否意味着没有SP1的应用程序即使具有相同名称(例如msvcr80.dll),也不会在具有SP1 dll的计算机上运行?

Also, I'm using Visual Studio 2005 and I read that the Service Pack 1 runtime is not backwards-compatible. Does this mean that an app built without SP1 will not run on a machine that has the SP1 dlls, even if they have the same name (e.g. msvcr80.dll)?

推荐答案

静态链接将使您的所有EXE和DLL膨胀,并可能导致崩溃(例如,如果一个DLL中的代码使用mDLL()在另一个DLL中分配的指针调用free())。

Linking statically will bloat all your EXEs and DLLs, and can cause crashes (eg. if code in one DLL calls free() with a pointer allocated by malloc() in a different DLL).

通过动态链接并将运行时DLL部署为私有程序集,您可以兼得两者。这只是意味着将包含运行时DLL及其清单的特制目录的副本放在可执行文件的旁边。

You can get the best of both worlds by linking dynamically and deploying the runtime DLLs as private assemblies. This simply means putting a copy of a specially-named directory containing the runtime DLLs and their manifests next to your executable.

请参见将Visual C ++库DLL私有部署一节程序集中的 http://msdn.microsoft.com/en -us / library / ms235291(VS.80).aspx 了解详细信息,但基本上您的应用程序如下所示:

See the section "Deploying Visual C++ library DLLs as private assemblies" at http://msdn.microsoft.com/en-us/library/ms235291(VS.80).aspx for details, but basically your application looks like this:

c:\Program Files\My App\MyApp.exe
c:\Program Files\My App\MyLibrary.dll
c:\Program Files\My App\Microsoft.VC80.CRT\Microsoft.VC80.CRT.manifest
c:\Program Files\My App\Microsoft.VC80.CRT\msvcr80.dll

关于最后一个问题,是的,目标计算机需要正确版本的运行时DLL才能工作,但是通过将它们部署为私有程序集,您可以保证

As to your last question, yes, the target machine needs the correct versions of the runtime DLLs to work, but by deploying them as private assemblies, you guarantee that.

另一个好处是非管理员用户可以安装您的应用程序(不在Progra中m个文件,但在其他位置)-他们不需要权限就可以将文件写入WinSxS区域。

Another benefit is that non-admin users can install your app (not into Program Files, but elsewhere) - they don't need permission to write files into the WinSxS area.

这篇关于我应该静态或动态链接到Visual Studio C运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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