简体   繁体   English

独立VS 2010 C ++程序

[英]Standalone VS 2010 C++ Program

it's been a long while since I've used VS 2010 and C++, and as I'm getting back to using it, I'm running into the same problems that plagued me last year: the exe's that I compile do not run well on older machines that do not have the correct C++ runtimes. 自从我使用VS 2010和C ++以来已经有很长时间了,而当我重新使用它时,我遇到了去年困扰我的同样问题:我编译的exe不能很好地运行没有正确的C ++运行时的较旧计算机。 I do not even know what link to give them, and I told them to install this after they had an error that said "The program can't start because MSVCR100.dll is missing from your computer. Try Reinstalling the program to fix this problem. Click OK to close the application."). 我什至不知道该给他们什么链接,当他们遇到一个错误消息“您的计算机上缺少MSVCR100.dll导致程序无法启动,请告诉我,然后告诉他们安装 。尝试重新安装该程序来解决此问题”。单击“确定”关闭该应用程序。”) So I went in and set the code generation to /MT and disabled quite a few options, and tried messing around with lots of options, but still the same result. 因此,我将代码生成设置为/ MT并禁用了很多选项,并尝试使用很多选项,但结果仍然相同。

My question is: Is there a list of complete VS 2010 C++ distributables that I can just give and tell them to install so all of the C++ programs I compile on my VS 2010 will work on Windows XP, or even better, a way to general a standalone exe that contains all it needs to work, and does not rely on DLLs? 我的问题是:是否可以提供完整的VS 2010 C ++分发列表,并告诉他们进行安装,以便我在VS 2010上编译的所有C ++程序都可以在Windows XP上运行,甚至更好,这是通用的一种方法一个独立的exe,包含所有需要的工作,并且不依赖DLL? I'm thinking like linking to a library that has everything the exe references. 我在想链接到包含exe引用的所有内容的库。 If it helps, I'm building for both x64 and x86. 如果有帮助,我将同时为x64和x86进行构建。

PS What's up with the manifest file, should I include it or not? PS清单文件有什么问题,我应该包括吗?

The easiest thing to do is to just install the VC++ Redistributable Package. 最简单的方法是仅安装VC ++可再发行组件包。 It has both x86 and x64 versions. 它具有x86x64版本。

Firstly, before I actually give you the detail: 首先,在我实际提供详细信息之前:


Warning 警告

If you do this, things will be bad for two reasons: 如果执行此操作,则由于以下两个原因会造成问题:

  1. If there are security or other bugs in the MSVC runtimes, and you take this approach, they're baked into your app which means you need to re-distribute. 如果MSVC运行时中存在安全性或其他错误,而您采用这种方法,则会将它们烘焙到您的应用程序中,这意味着您需要重新分发。 DLLs are preferred because theoretically people use system update which means any errors get fixed. 首选DLL,因为从理论上讲,人们使用系统更新,这意味着所有错误都会得到解决。
  2. Everything else you compile into your exe also needs to do this. 您编译到exe中的其他所有内容也都需要这样做。 If you don't, you end up with two versions of the code and whatever you're using won't link. 如果不这样做,最终将得到两个版本的代码,并且所使用的任何内容都不会链接。

One possible solution is to bake the MSVC runtime into your application, by using the cl.exe option (C/C++ compiler settings) /MT which means multi-threaded version of the C/C++ runtime linked statically. 一种可能的解决方案是使用cl.exe选项(C / C ++编译器设置) /MT将MSVC运行时烘焙到您的应用程序中,这意味着静态链接的C / C ++运行时的多线程版本。 As I said, if you try to link against something that is linked itself dynamically to the runtime, you're going to end up in a mess. 就像我说的,如果您尝试链接本身动态链接到运行时的内容,那么最终将陷入混乱。 Also, as I said, this represents an additional security risk factor, so bear that in mind. 另外,正如我所说,这是一个额外的安全风险因素,请记住这一点。

The other options are to write an installer that can either download the appropriate runtime, or include the DLL needed. 其他选项是编写一个安装程序,该安装程序可以下载适当的运行时,也可以包括所需的DLL。

If you're using some feature of the runtime that exceeds a certain version of Windows (generic statement, but it does happen) then you should be able to use the Windows SDK to target various versions of Windows using appropriate C runtimes. 如果您使用的运行时功能超出了Windows的特定版本(通用语句,但是确实发生了),那么您应该能够使用适当的C运行时使用Windows SDK来定位Windows的各种版本。

http://www.microsoft.com/downloads/en/details.aspx?FamilyID=a7b7a05e-6de6-4d3a-a423-37bf0912db84 http://www.microsoft.com/downloads/zh-CN/details.aspx?FamilyID=a7b7a05e-6de6-4d3a-a423-37bf0912db84

google text: visual studio c++ redist Google文字:Visual Studio C ++ Redist

Do not statically link to the runtime; 不要静态链接到运行时。 specifically don't do so if you're using any kind of dll for other purposes. 如果您将任何类型的dll用于其他目的,则不要这样做。 It introduces all kinds of bogus problems wrt heap management that you probably don't want to mess with. 它介绍了您可能不想惹的堆管理问题。

Open the properties dialog for your project and select Configuration Properties | 打开项目的属性对话框,然后选择配置属性| C/C++ | C / C ++ | Code Generation. 代码生成。 The default setting is Multi-threaded DLL. 默认设置为多线程DLL。 Change that to Multi-threaded and you'll be building and .EXE with the run-time statically linked in. Don't forget to do the same for the debug version. 将其更改为“多线程”,您将使用静态链接的运行时构建和.EXE。不要忘记对调试版本执行相同的操作。

If you're using MFC or ATL, you will need to navigate to Configuration Properties | 如果您使用的是MFC或ATL,则需要导航至“配置属性” |“配置”。 General and set "Use of MFC" or "Use of ATL" to link statically as well. 常规,并将“使用MFC”或“使用ATL”设置为静态链接。

NB: If you link the runtime statically, you must make sure that any other library you're linking in also links it in statically. 注意:如果您静态链接运行时,则必须确保要链接的任何其他库也都以静态方式链接。 Otherwise you'll wind up with two copies of the runtime in memory, each with its own heap and bad things will happen when code using one runtime tries to free an object allocated by the other runtime. 否则,您将在内存中获得两个运行时副本,每个副本都有其自己的堆,并且当使用一个运行时的代码尝试释放另一个运行时分配的对象时,将会发生不好的事情。

This previous answer should hold true for VS2010. 这个先前的答案应该适用于VS2010。 I still build with VS2005, but all my apps use the static CRT for the sole reason of being able to run across old and newer machines alike. 我仍然使用VS2005进行构建,但是我所有的应用程序都使用静态CRT,其唯一原因是能够同时在新旧计算机上运行。

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

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