简体   繁体   English

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

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

I am a Microsoft Visual Studio user. 我是Microsoft Visual Studio用户。 My question is about the "C/C++ Runtime Library". 我的问题是关于“ 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"? “ C / C ++运行时库”和“ C / C ++标准库”之间有区别吗?

  2. How do I know if the "C/C++ Runtime Library" library is statically or dynamically linked to the project? 我如何知道“ C / C ++运行时库”库是静态链接还是动态链接到项目?

  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? 如果“ C / C ++运行时库”是动态链接到项目的,我如何知道使用了哪个“ .dll”以及文件系统中所使用的“ .dll”在哪里?

  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)? 假设我将“ C / C ++运行时库”静态链接到项目,可以确定从源代码生成的可执行文件将在所有Windows平台(XP / Vista / Seven / ...,32位/ 64)上都可以运行位)?

  6. What are the advantages/drawbacks of dynamically linking the "C/C++ Runtime Library" to the project? 将“ C / C ++运行时库”动态链接到项目的优点/缺点是什么?

  7. Should the "C/C++ Runtime Library" rather be statically or dynamically linked to the project? 应该将“ C / C ++运行时库”静态还是动态链接到项目?

The term "C/C++ Runtime Library" doesn't mean anything, it is roughly the name of a project setting in the IDE. 术语“ C / C ++运行时库”没有任何意义,它大致是IDE中项目设置的名称。 Project + Properties, C/C++, Code Generation, Runtime Library setting. 项目+属性,C / C ++,代码生成,运行时库设置。 There you can choose between /MD and /MT. 您可以在/ MD和/ MT之间进行选择。

With /MD, the default setting, your program will be using the DLL version of the runtime libraries. 使用默认设置/ MD,您的程序将使用运行时库的DLL版本。 On your machine they were copied into c:\\windows\\system32 and/or c:\\windows\\syswow64 by the Visual Studio installer. 在您的计算机上,它们已由Visual Studio安装程序复制到c:\\ windows \\ system32和/或c:\\ windows \\ syswow64。 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. 并且您在VS安装目录的vc / redist子目录中有它们的副本,在为程序创建安装程序时可以在其中使用。 Three versions of them, x86 for 32-bit Intel processors, x64 for 64-bit Intel processors and arm for ARM processors. 它们的三个版本分别是用于32位Intel处理器的x86,用于64位Intel处理器的x64和用于ARM处理器的arm。 Pick the right one based on the Platform you selected in your project. 根据您在项目中选择的平台选择合适的平台。

The relevant DLL names are: 相关的DLL名称是:

  • msvcr110.dll : the C runtime library (memcpy et al) msvcr110.dll:C运行时库(memcpy等)
  • msvcp110.dll : the C++ standard library (std::string et al) msvcp110.dll:C ++标准库(std :: string等)
  • vccorlib110.dll : the runtime library for Windows Store applications vccorlib110.dll:Windows应用商店应用程序的运行时库
  • vcomp110.dll : the runtime library for OpenMP (see #pragma omp) vcomp110.dll:OpenMP的运行时库(请参阅#pragma omp)
  • atl110.dll : the runtime library for ATL projects atl110.dll:ATL项目的运行时库
  • mfc110*.dll : runtime and localization libraries for MFC projects mfc110 * .dll:MFC项目的运行时和本地化库
  • vcamp110.dll : the runtime library for AMP projects 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. 在您的计算机上,您还获得了这些DLL的调试版本,并由VS安装程序复制到Windows目录中。 They have the same name with the letter "d" appended. 它们具有相同的名称,并附加字母“ d”。 Useful only to debug your code, you can't redistribute them. 仅对调试代码有用,不能重新分发它们。 The corresponding Runtime Library setting is /MDd. 相应的运行时库设置为/ 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. 将所有这些DLL安装在用户计算机上的一种简单方法是使用预构建的安装程序。 You can download it from here (note: current only as of today, this may change when a service pack or update becomes available). 您可以从此处下载(注意:仅从今天开始有效,当Service Pack或更新可用时,它可能会更改)。 Or you can simply copy them into the same directory as your main EXE. 或者,您可以简单地将它们复制到与主EXE相同的目录中。

You can avoid taking a dependency on these DLLs by changing the Runtime Library setting to /MT. 通过将运行时库设置更改为/ MT,可以避免依赖这些DLL。 In which case the runtime support code is linked into your program and you'll have only a single EXE to deploy. 在这种情况下,运行时支持代码将链接到您的程序,并且只有一个EXE可以部署。 It will of course get bigger when you do so, sometimes significantly so, especially when you use MFC. 当您这样做时,它当然会变大,有时会变大,尤其是在使用MFC时。

Using /MT is risky if you create DLLs as well as an EXE. 如果创建DLL和EXE,则使用/ MT会有风险。 You'll end up with multiple copies of the CRT in your program. 您最终将在程序中获得CRT的多个副本。 This was especially a problem with earlier versions of VS where each CRT would get its own heap, not so much with VS2012. 对于VS的早期版本尤其如此,每个CRT都有自己的堆,而VS2012则不然。 But you can still have ugly runtime problems when you have more than one "errno" variable for example. 但是,例如,当您有多个“ errno”变量时,仍然会遇到难看的运行时问题。 Using /MD is highly recommended to avoid such lossage. 强烈建议使用/ 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. 您的程序将在Windows Vista,7和8上运行。对XP的支持正在减弱,您将需要VS Update 1并将项目中的工具集设置从“ v110”更改为“ v110_xp”,以创建仍可在XP上运行的程序。 Some functionality is lost when you do so, associated with locale and thread-local storage, testing is required. 这样做会丢失某些功能,并且与语言环境和线程本地存储相关联,因此需要进行测试。

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). 有时人们使用运行时库来表示所有内容,而完全忽略标准库(对于Microsoft工具)。 However, technically, the runtime library is loaded at runtime, so it includes the pair .lib (import lib) and .dll. 但是,从技术上讲,运行时库是在运行时加载的,因此它包括.lib(导入lib)和.dll对。 See here for details: http://msdn.microsoft.com/en-us/library/vstudio/abx4dbyh(v=vs.100).aspx 有关详细信息,请参见此处: http : //msdn.microsoft.com/zh-cn/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(如果安装了更高版本的Windows sdk)或Visual C ++目录的lib目录中。

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/ http://www.dependencywalker.com/

The DLLs are somewhere in the Windows directory. DLL位于Windows目录中的某个位置。 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).aspx http://en.wikipedia.org/wiki/Side-by-side_assembly 有关详细信息: http : //msdn.microsoft.com/zh-cn/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可再发行与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. 是的,如果您静态链接,则在无法找到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中,而编译为您的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. 较小的exe大小,因为所有库文件都位于dll中,这应该已经在用户的系统上安装了,尽管有时并非如此。

b - If there are bugs in the runtime, Microsoft can push a new release down to the user. b-如果运行时存在错误,Microsoft可以将新版本下推给用户。 You don't have to deal with it. 您不必处理它。 If you statically link, you have to push a new exe to the user. 如果您静态链接,则必须向用户推送新的exe。

Why not to use dll: 为什么不使用dll:

a - many issues with dealing with dll. 一个-处理dll的许多问题。 If you forget to bundle the redist, many problems can show up. 如果您忘记捆绑重做器,则会出现许多问题。

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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM