Microsoft Visual Studio ~ C/C++ 运行时库 ~ 静态/动态链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14749662/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:39:31  来源:igfitidea点击:

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

c++visual-studiomicrosoft-runtime-library

提问by Léa Massiot

I am a Microsoft Visual Studio user. My question is about the "C/C++ Runtime Library".

我是 Microsoft Visual Studio 用户。我的问题是关于“C/C++ 运行时库”。

I created an "Empty Project" with a ".cpp" source file "main.cpp" containing the following code:

我使用包含以下代码的“.cpp”源文件“main.cpp”创建了一个“空项目”:

#include <iostream>

int main(void)
{
    std::cout << "Hello World" << std::endl;
    return 0;
}

"iostream is a header file which is used for input/output in the C++ programming language. It is part of the C++ standard library."

“iostream 是 C++ 编程语言中用于输入/输出的头文件。它是 C++ 标准库的一部分。”

  1. Is there a difference between "C/C++ Runtime Library" and "C/C++ Standard Library"?

  2. How do I know if the "C/C++ Runtime Library" library is statically or dynamically linked to the project?

  3. How do I know where this library is located in the filesystem?

  4. In case, the "C/C++ Runtime Library" is dynamically linked to the project, how can I know which ".dll" is used and where the used ".dll" is located in the filesystem?

  5. Suppose that I statically link the "C/C++ Runtime Library" to the project, can I be sure that the executable generated from the source code will work on all Windows Platforms (XP/Vista/Seven/..., 32 bit/64 bit)?

  6. What are the advantages/drawbacks of dynamically linking the "C/C++ Runtime Library" to the project?

  7. Should the "C/C++ Runtime Library" rather be statically or dynamically linked to the project?

  1. “C/C++ 运行时库”和“C/C++ 标准库”之间有区别吗?

  2. 我如何知道“C/C++ 运行时库”库是静态链接还是动态链接到项目?

  3. 我怎么知道这个库在文件系统中的位置?

  4. 如果“C/C++ 运行时库”动态链接到项目,我如何知道使用了哪个“.dll”以及所使用的“.dll”在文件系统中的位置?

  5. 假设我将“C/C++ 运行时库”静态链接到项目,我能否确定从源代码生成的可执行文件将适用于所有 Windows 平台(XP/Vista/Seven/...,32 位/64少量)?

  6. 将“C/C++ 运行时库”动态链接到项目的优点/缺点是什么?

  7. “C/C++ 运行时库”应该静态还是动态链接到项目?

回答by Hans Passant

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.

术语“C/C++ 运行时库”没有任何意义,它大致是 IDE 中项目设置的名称。项目 + 属性、C/C++、代码生成、运行时库设置。在那里您可以在 /MD 和 /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.

使用默认设置 /MD,您的程序将使用运行时库的 DLL 版本。在你的机器上,它们被 Visual Studio 安装程序复制到 c:\windows\system32 和/或 c:\windows\syswow64。您在 VS 安装目录的 vc/redist 子目录中有它们的副本,供您在为程序创建安装程序时使用。它们的三个版本,x86 用于 32 位 Intel 处理器,x64 用于 64 位 Intel 处理器,arm 用于 ARM 处理器。根据您在项目中选择的平台选择正确的平台。

The relevant DLL names are:

相关的 DLL 名称是:

  • 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
  • msvcr110.dll:C 运行时库(memcpy 等)
  • msvcp110.dll:C++ 标准库(std::string 等)
  • vccorlib110.dll :Windows Store 应用程序的运行时库
  • vcomp110.dll :OpenMP 的运行时库(参见 #pragma omp)
  • atl110.dll : ATL 项目的运行时库
  • mfc110*.dll:MFC 项目的运行时和本地化库
  • vcamp110.dll:AMP 项目的运行时库

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.

在您的机器上,您还获得了这些 DLL 的调试版本,由 VS 安装程序复制到 Windows 目录中。它们具有相同的名称,并附加了字母“d”。仅用于调试您的代码,您不能重新分发它们。相应的运行库设置是 /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.

大多数 C++ 项目只需要 msvcr110.dll 和 msvcp110.dll,您知道何时选择使用其他库,因为它们有特定的项目模板和设置。

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.

将所有这些 DLL 安装到用户计算机上的简单方法是使用预构建的安装程序。您可以从这里下载它(注意:仅从今天开始,这可能会在服务包或更新可用时发生变化)。或者您可以简单地将它们复制到与主 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.

您可以通过将运行时库设置更改为 /MT 来避免依赖这些 DLL。在这种情况下,运行时支持代码会链接到您的程序中,您将只需要部署一个 EXE。当您这样做时,它当然会变得更大,有时会变得更大,尤其是当您使用 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.

如果您创建 DLL 和 EXE,则使用 /MT 是有风险的。您最终会在您的程序中获得多个 CRT 副本。这对于早期版本的 VS 尤其是一个问题,其中每个 CRT 都会获得自己的堆,而 VS2012 则没有那么多。但是,例如,当您有多个“errno”变量时,您仍然会遇到难看的运行时问题。强烈建议使用 /MD 以避免此类损失。

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.

您的程序将在 Windows Vista、7 和 8 上运行。对 XP 的支持正在减弱,您将需要 VS Update 1 并将项目中的工具集设置从“v110”更改为“v110_xp”以创建仍可在 XP 上运行的程序. 这样做会丢失一些与区域设置和线程本地存储相关的功能,因此需要进行测试。

回答by thang

Here goes nothing... please chime in if you find a mistake.

这里什么都没有...如果你发现错误,请加入。

1. Is there a difference between "C/C++ Runtime Library" and "C/C++ Standard Library"?

1、“C/C++运行库”和“C/C++标准库”有区别吗?

Yes and no. Sometimes people use runtime library to mean everything and ignore standard library altogether (for Microsoft tools). However, technically, the runtime library is loaded at runtime, so it includes the pair .lib (import lib) and .dll. See here for details: http://msdn.microsoft.com/en-us/library/vstudio/abx4dbyh(v=vs.100).aspx

是和否。有时人们使用运行时库来表示一切,而完全忽略标准库(对于 Microsoft 工具)。但是,从技术上讲,运行时库是在运行时加载的,因此它包含 .lib(导入库)和 .dll 对。有关详细信息,请参见此处:http: //msdn.microsoft.com/en-us/library/vstudio/abx4dbyh(v=vs.100).aspx

Technically, the libc* are standard libraries and the *crt are runtime libraries.

从技术上讲,libc* 是标准库,*crt 是运行时库。

2. How do I know if the "C/C++ Runtime Library" library is statically or dynamically linked to the project?

2.如何知道“C/C++运行库”库是静态链接还是动态链接到项目?

If you're using the IDE (VS2010, others are similar), this is in project properties:

如果您使用的是 IDE(VS2010,其他类似),这是在项目属性中:

-  configuration properties
        - c/c++
               - code generation
                      [Runtime Library]

3. How do I know where this library is located in the filesystem?

3. 我怎么知道这个库在文件系统中的位置?

The lib files are in the lib dir of your sdk (if you installed a later windows sdk) or the Visual C++ directory.

lib 文件位于 sdk 的 lib 目录中(如果您安装了更高版本的 windows sdk)或 Visual C++ 目录。

4. In case, the "C/C++ Runtime Library" is dynamically linked to the project, how can I know which ".dll" is used and where the used ".dll" is located in the filesystem?

4. 如果“C/C++ 运行时库”是动态链接到项目的,我如何知道使用了哪个“.dll”以及所使用的“.dll”在文件系统中的位置?

You can figure out which ones are used by using the depends tool. http://www.dependencywalker.com/

您可以使用depends 工具找出使用了哪些。 http://www.dependencywalker.com/

The DLLs are somewhere in the Windows directory. They move them around and it's now in funky places with manifests and stuff to keep track of version. I wouldn't worry about this too much. If you have to worry about this, something's probably wrong. For details: http://msdn.microsoft.com/en-us/library/windows/desktop/aa375365(v=vs.85).aspxhttp://en.wikipedia.org/wiki/Side-by-side_assembly

DLL 位于 Windows 目录中的某个位置。他们四处移动它们,现在它位于时髦的地方,带有清单和用于跟踪版本的东西。我不会太担心这个。如果您必须担心这一点,则可能有问题。详情:http: //msdn.microsoft.com/en-us/library/windows/desktop/aa375365( v= vs.85).aspx http://en.wikipedia.org/wiki/Side-by-side_assembly

If this is a concern, you can bundle a redistributable package with your installer: Difference between Visual Studio Redistributable and Visual Studio SP1

如果这是一个问题,您可以将一个可再发行包与您的安装程序捆绑在一起:Visual Studio Redistributable 和 Visual Studio SP1 之间的区别

5. Suppose that I statically link the "C/C++ Runtime Library" to the project, can I be sure that the executable generated from the source code will work on all Windows Platforms (XP/Vista/Seven/..., 32 bit/64 bit)?

5. 假设我将“C/C++ 运行时库”静态链接到项目,我是否可以确定从源代码生成的可执行文件将适用于所有 Windows 平台(XP/Vista/Seven/...,32 位) /64 位)?

Yes, if you link statically, then you're safer in terms of not being able to find the dll. However, this makes your executable larger. There are other consequences in terms of behavior... It's difficult to enumerate, but the difference comes from the fact that the library is in a dll vs compiled into your exe.

是的,如果您静态链接,那么就无法找到 dll 而言,您会更安全。但是,这会使您的可执行文件更大。在行为方面还有其他后果......很难枚举,但不同之处在于该库位于 dll 中,而不是编译到您的 exe 中。

6. What are the advantages/drawbacks of dynamically linking the "C/C++ Runtime Library" to the project?

6. 动态链接“C/C++运行库”到项目有什么优点/缺点?

Why use dll:

为什么使用dll:

a - size. smaller exe size because all the library stuff is in the dll which are supposed to have been installed already on the user's system, although this is sometimes not true.

a - 大小。较小的 exe 大小,因为所有库内容都在 dll 中,这些内容应该已安装在用户系统上,尽管有时并非如此。

b - If there are bugs in the runtime, Microsoft can push a new release down to the user. You don't have to deal with it. If you statically link, you have to push a new exe to the user.

b - 如果运行时存在错误,Microsoft 可以向用户推送新版本。你不必处理它。如果静态链接,则必须向用户推送新的 exe。

Why not to use dll:

为什么不使用dll:

a - many issues with dealing with dll. If you forget to bundle the redist, many problems can show up.

a - 处理 dll 的许多问题。如果忘记捆绑 redist,就会出现很多问题。

b - having more dlls to load and unload causes slower start up and exit time.

b - 加载和卸载更多 dll 会导致启动和退出时间变慢。

Probably other reasons that I haven't thought of...

可能还有其他我没想到的原因……

7. Should the "C/C++ Runtime Library" rather be statically or dynamically linked to the project?

7. “C/C++ 运行库”应该静态链接还是动态链接到项目?

It really depends. I personally prefer statically linked. I hate scrambling around looking for the right redist/dll/etc.

这真的取决于。我个人更喜欢静态链接。我讨厌四处寻找合适的 redist/dll/etc。