windows 在没有运行时可再发行组件的情况下运行 C++ 二进制文件(Server2k3、XPSP3)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1581105/
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
Running C++ binaries without the runtime redistributable (Server2k3, XPSP3)
提问by Mike Trader
Having written a CGI application in Visual Studio 2008 and debigged it locally, I uploaded it to a Windows Server 2003 OS where it promptly failed to run.
在 Visual Studio 2008 中编写了一个 CGI 应用程序并在本地对其进行调试后,我将其上传到 Windows Server 2003 操作系统,但它立即无法运行。
I am guessing I need to install the wretched Runtime distributable, but after reading this:
我猜我需要安装可恶的 Runtime 发行版,但在阅读此内容后:
http://kobyk.wordpress.com/2007/07/20/dynamically-linking-with-msvcrtdll-using-visual-c-2005/
http://kobyk.wordpress.com/2007/07/20/dynamically-linking-with-msvcrtdll-using-visual-c-2005/
I am wondering if it makes more sense to ignore this side by side stuff and just re-write the app.
我想知道忽略这些并排的东西并重新编写应用程序是否更有意义。
I am guessing Windows Server 2003 does not have MSCRVT version I need? Does Windows Server 2003 have it?
我猜 Windows Server 2003 没有我需要的 MSCRVT 版本?Windows Server 2003 有吗?
When it comes to deploying thick clients, I would like to distribute the required dlls with my app. What are they assuming I just INCLUDE iostream, sstream, string?
在部署胖客户端时,我想用我的应用程序分发所需的 dll。他们假设我只包含 iostream、sstream、string 是什么?
Does it change significantly if I add windows.h?
如果我添加 windows.h,它会发生显着变化吗?
Added:
添加:
Using the /MT switch recommended below
使用下面推荐的 /MT 开关
C / C++ -> Code Generation -> Runtime Library -> Multi-threaded(/MT)
C/C++ -> 代码生成 -> 运行时库 -> 多线程(/MT)
(You will probably need to do a clean:
(您可能需要进行清理:
Build -> Clean
构建 -> 清洁
in order to avoid the error message
为了避免错误信息
"Failed to save the updated manifest to the file")
“未能将更新的清单保存到文件中”)
bloated my app from 38k to 573k. Thats what I call Significant (imagine if that were your salary). Since many instances of this app will be loaded and unloaded constantly (requiring precious memory and processor resources) I would like to find a better (smaller) solution.
使我的应用程序从 38k 膨胀到 573k。这就是我所说的重要(想象一下,如果那是你的薪水)。由于此应用程序的许多实例将不断加载和卸载(需要宝贵的内存和处理器资源),我想找到一个更好(更小)的解决方案。
I understand this is not important for many situations today and not the focus of many developers, hence the trend to .NOT and 60MB runtimes, but this is what I want to do.
我知道这对于当今的许多情况并不重要,也不是许多开发人员的重点,因此趋向于 .NOT 和 60MB 运行时,但这就是我想要做的。
Added:
添加:
After removing the debugging to get the project to compile:
去掉调试后得到项目编译:
Project -> Propeties -> c/c++ -> Preprocessor -> Preprocessor Definitions (remove DEBUG;)
项目 -> 属性 -> c/c++ -> 预处理器 -> 预处理器定义(删除调试;)
the size was reduced to 300k, and it will run.
大小减少到 300k,它会运行。
Added: As suggested by Chris Becke below, copying: msvcm90.dll msvcp90.dll msvcr90.dll Microsoft.VC90.CRT.manifest To the directory of the application will provide all the runtime needed.
添加:正如下面 Chris Becke 所建议的,复制: msvcm90.dll msvcp90.dll msvcr90.dll Microsoft.VC90.CRT.manifest 到应用程序的目录将提供所有需要的运行时。
Using Visual Studio 6 has been suggested a few times, but it does not support Vista (or Windows 7 we assume) Other solutions that do not require a runtime distributable would probably me MASM or even a flavor of Basic. Unfortunately that defeats the purpose of using a high level OOP language like C++.
已多次建议使用 Visual Studio 6,但它不支持 Vista(或我们假设的 Windows 7) 不需要运行时可分发的其他解决方案可能是 MASM 甚至是 Basic 风格。不幸的是,这违背了使用像 C++ 这样的高级 OOP 语言的目的。
So long as I do need to require the C++ redistributable be installed, the trade off is an additional 260k. Thats acceptable
只要我确实需要安装 C++ 可再发行组件,代价就是额外的 260k。可以接受
采纳答案by Chris Becke
A fuller list of options:
更完整的选项列表:
- Rewrite the app so that theres no C/C++ usage at all.
- Switch to Visual Studio 6 or a mingw based toolset like Code::Blocks - these use the already distributed msvcrt.dll as their runtime.
- Build using the /MT switch. This builds the necessary runtime functions into your exe. Which will bloat it. But that bloat (frankly) is less overhead than loading separate dll's.
- Distribute the VS9 runtime as a 'private sxs' install. This entails copying the contents of
C:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT
into the same folder as your application's exe. If you have applied SP1, then this folder will contain the SP1 runtime, but your application will be asking for the RTM runtime (go figure). Add_BIND_TO_CURRENT_CRT_VERSION
to your projects defines, rebuild and that should sort itself out. - There is apparently a vc_redist.exe that can be obtained for VS9. Find that or figure out the MSI or installer mojo required to actually install the above assembly (being the contents of Microsoft.VC90.CRT) into the shared sxs store.
- 重写应用程序,以便根本不使用 C/C++。
- 切换到 Visual Studio 6 或基于 mingw 的工具集(如 Code::Blocks)——它们使用已经分发的 msvcrt.dll 作为它们的运行时。
- 使用 /MT 开关构建。这会将必要的运行时函数构建到您的 exe 中。这会使它膨胀。但是这种膨胀(坦率地说)比加载单独的 dll 的开销要小。
- 将 VS9 运行时作为“私有 sxs”安装分发。这需要将 的内容复制
C:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT
到与应用程序的 exe 相同的文件夹中。如果您已应用 SP1,则此文件夹将包含 SP1 运行时,但您的应用程序将要求 RTM 运行时(见图)。添加_BIND_TO_CURRENT_CRT_VERSION
到您的项目中,定义,重建,这应该自己解决。 - 显然有一个可以为 VS9 获得的 vc_redist.exe。找到或找出将上述程序集(即 Microsoft.VC90.CRT 的内容)实际安装到共享 sxs 存储中所需的 MSI 或安装程序 mojo。
回答by Roland Rabien
Let me revise my answer:
让我修改我的答案:
You have a few options:
您有几个选择:
1) Install the VC++2008 runtime. You can downloadand installer for them. Merge modules for them are also available.
1) 安装 VC++2008 运行时。您可以为它们下载和安装程序。它们的合并模块也可用。
2) Link against the runtime statically (/MT or C/C++ -> Code Generation -> Runtime Library: Multi-threaded). This will increase the size of your executable, but it won't depend on any Dlls. If you are worried about size, only use the C standard library or only use the Windows API directly.
2) 静态链接运行时(/MT 或 C/C++ -> 代码生成 -> 运行时库:多线程)。这将增加可执行文件的大小,但它不依赖于任何 Dll。如果您担心大小,请仅使用 C 标准库或仅直接使用 Windows API。
3) Use an older version of VC++. 2005 and earlier did not require the runtime to be installed. The Dlls could be placed in the same directory as the executable. If you use VC++6.0 the runtimes will be installed on all versions of windows, otherwise you need to install it yourself.
3) 使用旧版本的 VC++。2005 及更早版本不需要安装运行时。Dll 可以与可执行文件放在同一目录中。如果你使用VC++6.0,所有版本的windows都会安装runtimes,否则需要自己安装。