Visual Studio 2010 MSVCR 依赖项删除?
我试过用谷歌搜索这个,但找不到解决方案。我正在尝试学习一些基本的 C++。我写了一个简单的 hello world:
#include <stdio.h>
int main()
{
printf("hello, world\n");
return 0;
}
它编译完美,一切顺利!我想太棒了,所以我用 XP 加载了虚拟机,但没有安装任何服务包,然后尝试运行它。它告诉我我需要 MSVCR dll。有什么办法可以完全消除这种依赖吗?我不想用 dll 填充程序。我希望它彻底消失。是否可以制作并运行一个在 XP 及更高版本中运行的程序?谢谢。
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\n");
return 0;
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从技术上来说,在 C 中消除这种依赖是可能的,但我不确定在 C++ 中是否可能。无论哪种情况,我都不会推荐它。你会失去 CRT 在幕后为你做的很多事情,其中大部分是你不想以低劣的方式重塑自己的。对于初学者来说,运行时库实际上调用
main
函数以及调用全局和静态 C++ 对象的构造函数和析构函数。最好和最简单的解决方案可能是更改应用程序链接到运行时库的方式。您有两种不同的选择:动态和静态。动态链接的内存效率更高,意味着您的应用程序将利用对库进行的任何错误修复。它依赖于存在的运行时 DLL 才能启动您的应用程序。静态链接实际上是在构建的链接阶段将运行时库代码嵌入到您的应用程序中。这意味着您可以在不分发 DLL 的情况下运行,但有一些重要的注意事项。
对于简单的应用程序,这些警告不太可能相关。更改项目选项中使用的链接样式:
在“运行时库”属性组合框中,选择“多线程”选项之一。
调试版本应使用“多线程调试”,而发布版本应使用“多线程”。
请注意,由于您使用的是 VS 2010,您仍然可以选择动态链接到运行时并获得所有这样做的优点是无需在目标计算机上运行 CRT 安装程序。您所需要的只是将可再发行的 DLL 放入与应用程序的可执行文件相同的文件夹中。这使得部署(甚至测试)变得非常简单和直接。您会发现这些库是 Visual Studio 安装的一部分:
当然,CRT 的调试版本永远是不可重新分发的。由于您不应该分发应用程序的调试版本,因此这不是问题。确保您已经编译了“发布”版本(使用顶部工具栏中的下拉组合框),为此您只需要在上述目录中找到的可再发行库。
任何版本的 Windows 都没有可供您使用的 C 运行时。 Windows 本身确实依赖于 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.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:
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".
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:
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.
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.
您可以静态链接MS运行时,项目选项-> C/C++->代码生成->多线程(或多线程调试用于调试配置)。那么就不需要 DLL 了。
You can link the MS runtime statically, Project Options -> C/C++ -> Code Generation -> Multithreaded (or Multithreaded Debug for debugging configuration). No DLL should be needed then.
您可以删除烦人的运行时库,执行以下操作:
项目属性>链接器>输入>忽略所有默认库>是的
这会给你一些需要处理的安静问题,例如,浮动
点数不起作用,堆栈内存非常小(大约3k),没有针对缓冲区溢出等的内置帮助,并且如果不将标准库复制粘贴到您的系统中,则无法使用标准库项目。
这也会减小
.exe
的大小,几乎相当于手工制作的大小在装配中。
you can remove the annoying run-time library, do this:
project properties > linker > input > ignore all default libraries> yes
this will give you quiet a few issues you need to deal with, for example, floating
point numbers won't work, stack memory is very small (about 3k), there's no built in help against buffer overflows and such, and you can't use the standard library without copy pasting it in your project.
this will also decrease the size of the
.exe
nearly equivalent to as if it was hand madein assembly.