Microsoft Visual Studio ~ C/C++ Runtime Library ~ Static/dynamic linking

本文详细解析了 C/C++ Runtime Library 的概念及其在项目设置中的应用,包括不同运行库的选择(/MD 或 /MT)、相关 DLL 的名称(如 msvcr110.dll 和 msvcp110.dll)以及如何避免依赖 DLL 的风险。同时讨论了如何在不同的 Windows 操作系统版本上部署程序,并提供了安装所需 DLL 的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The term "C/C++ Runtime Library" doesn't mean anything, it is roughly the name of a project setting in the IDE. Project + Properties, C/C++, Code Generation, Runtime Library setting. There you can choose between /MD and /MT.

With /MD, the default setting, your program will be using the DLL version of the runtime libraries. On your machine they were copied into c:\windows\system32 and/or c:\windows\syswow64 by the Visual Studio installer. And you've got copies of them in the vc/redist subdirectory of the VS install directory, there for you to use when you create an installer for your program. Three versions of them, x86 for 32-bit Intel processors, x64 for 64-bit Intel processors and arm for ARM processors. Pick the right one based on the Platform you selected in your project.

The relevant DLL names are:

  • msvcr110.dll : the C runtime library (memcpy et al)
  • msvcp110.dll : the C++ standard library (std::string et al)
  • vccorlib110.dll : the runtime library for Windows Store applications
  • vcomp110.dll : the runtime library for OpenMP (see #pragma omp)
  • atl110.dll : the runtime library for ATL projects
  • mfc110*.dll : runtime and localization libraries for MFC projects
  • vcamp110.dll : the runtime library for AMP projects

On your machine, you've also got the debug builds of those DLLs, copied to the Windows directory by the VS installer. They have the same name with the letter "d" appended. Useful only to debug your code, you can't redistribute them. The corresponding Runtime Library setting is /MDd.

Most C++ projects only need msvcr110.dll and msvcp110.dll, you'd know when you opt in to use the other libraries since there are specific project templates and settings for them.

A simple way to get all of these DLLs installed on your user's machine is to use the prebuilt installer. You can download it from here (note: current only as of today, this may change when a service pack or update becomes available). Or you can simply copy them into the same directory as your main EXE.

You can avoid taking a dependency on these DLLs by changing the Runtime Library setting to /MT. In which case the runtime support code is linked into your program and you'll have only a single EXE to deploy. It will of course get bigger when you do so, sometimes significantly so, especially when you use MFC.

Using /MT is risky if you create DLLs as well as an EXE. You'll end up with multiple copies of the CRT in your program. This was especially a problem with earlier versions of VS where each CRT would get its own heap, not so much with VS2012. But you can still have ugly runtime problems when you have more than one "errno" variable for example. Using /MD is highly recommended to avoid such lossage.

Your program will run on Windows Vista, 7 and 8. Support for XP is waning, you'll need VS Update 1 and change the toolset setting in the project from "v110" to "v110_xp" to create a program that still runs on XP. Some functionality is lost when you do so, associated with locale and thread-local storage, testing is required.

http://stackoverflow.com/questions/14749662/microsoft-visual-studio-c-c-runtime-library-static-dynamic-linking


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C++程序员Carea

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值