Visual Studio 2010 MSVCR 依赖删除?

我试过用 Google 搜索,但找不到解决方案.我正在尝试学习一些基本的 C++.我写了一个简单的hello world:

I've tried Googleing this but I could not find a solution. I am trying to learn some basic C++. I wrote a simple hello world:

#include <stdio.h>
int main()
{
    printf("hello, world
");
    return 0;
}

它完美地编译了一切!太好了,我想,所以我用 XP 加载了我的虚拟机,没有安装任何服务包,然后尝试运行它.它告诉我我需要 MSVCR dll.有什么办法可以完全消除这种依赖?我不想用 dll 填充程序.我希望它完全消失.是否可以制作和运行将在 XP 及更高版本中运行的程序?谢谢.

It compiled perfectly and everything! Great I thought, so I loaded up my virtual machine with XP and no service packs installed, then tried to run it. It told me I needed the MSVCR dll. Is there any way I can completely remove this dependency? I don't want to stuff the program with the dll. I want it to be gone, completely. Is it possible to make and run a program that will run in XP and up? Thanks.

推荐答案

在 C 中删除这种依赖在技术上是可能,但我不确定在 C++ 中是否可能.无论哪种情况,我都不会推荐它.你失去了很多 CRT 在幕后为你做的事情,其中??大部分你不想以低劣的方式重塑自己.对于初学者来说,实际调用 main 函数以及调用全局和静态 C++ 对象的构造函数和析构函数的是运行时库.

It is technically possible to remove this dependency in C, but I'm not sure it is even possible in C++. And in either case, I would not recommend it. You lose a lot of stuff that the CRT does for you behind the scenes, most of which you don't want to have to reinvent yourself in an inferior fashion. For starters, it's the runtime library that actually calls your main function as well as calling the constructors and destructors for global and static C++ objects.

最好和最简单的解决方案可能是更改您的应用程序链接到运行时库的方式.您有两种不同的选择:动态和静态.动态链接更节省内存,这意味着您的应用程序将利用对库进行的任何错误修复.它依赖于存在的运行时 DLL 才能启动您的应用程序.静态链接实际上是在构建的链接阶段将运行时库代码嵌入到您的应用程序中.这意味着您可以在不分发 DLL 的情况下运行,但有一些重要的注意事项.

The best and simplest solution is probably to change how your application links to the runtime libraries. You have two different options: dynamically and statically. Dynamic linking is more memory-efficient and means that your application will take advantage of any bug fixes that are made to the library. It relies on the runtime DLL being present in order for your app to start. Static linking actually embeds the runtime library code into your application during the link phase of the build. This means that you can run without distributing the DLL, but there are important caveats.

对于简单的应用,这些警告不太可能是相关的.更改项目选项中使用的链接样式:

For simple apps, it's unlikely that these caveats are relevant. Change the link style in use in your project's options:

  1. 在解决方案资源管理器中右键单击您的项目名称.
  2. 在左侧树形视图中展开C/C++"选项,然后选择代码生成"项.
  3. 在运行时库"属性组合框中,选择多线程"选项之一.
    调试版本应使用多线程调试",而发布版本应使用多线程".

  1. Right-click on your project name in the Solution Explorer.
  2. Expand the "C/C++" option in the left-hand treeview, and select the "Code Generation" item.
  3. In the "Runtime Library" property combobox, choose one of the "Multi-threaded" options.
    Debug builds should use "Multi-threaded Debug", while Release builds should use "Multi-threaded".

请注意,由于您使用的是 VS 2010,因此您仍然可以选择动态链接到运行时并获得这样做的所有优势,而无需在目标计算机上运行 CRT 安装程序.您所需要的只是将可再发行的 DLL 放置在与应用程序的可执行文件相同的文件夹中.这使得部署(甚至测试)变得非常简单直接.您会在 Visual Studio 安装中找到这些库:

Do note that since you're using VS 2010, you can still choose to dynamically link to the runtime and gain all of the advantages of doing so without having to run the CRT installer on the target machines. All you need is the redistributable DLL(s) placed into the same folder as your application's executable. This makes deploying (and even testing) very simple and straightforward. You'll find these libraries as part of your Visual Studio installation:

Program FilesVisual Studio x.0VCedist

当然,CRT 的调试版本从不可再分发.由于您不应该分发应用程序的调试版本,因此这不是问题.确保您已经编译了发布"版本(使用顶部工具栏中的下拉组合框),您只需要在上述目录中找到的可再发行库.

And of course, the debug versions of the CRT are never redistributable. Since you should not distribute debug versions of your application, this is not a problem. Make sure that you've compiled a "Release" build (using the drop-down combobox in the top toolbar), for which you will require only the redistributable libraries found in the above directory.

不能使用 XP 自带的运行时吗?

Can't I use the runtime that comes with XP?

任何版本的 Windows 都没有可供您使用的 C 运行时.Windows 本身确实依赖于 C 运行时库,但它部署了该库的 私有 版本供自己使用.应用程序无意链接到它或以任何方式使用它.您需要自己部署所有必要的依赖项,并且正如您所注意到的,您不能假设目标机器已经安装了正确的版本.

There is no C runtime for you to use that comes with any version of Windows. Windows itself indeed depends on a C runtime library, but it deploys a private version of that library for its own use. Applications are not intended to link to it or make use of it in any way. You're on your own for deploying all necessary dependencies, and as you've noticed, you cannot assume that the target machines will already have the correct version(s) installed.

相关文章