C++ 如何使用 Visual Studio Express 2005 制作完全静态链接的 .exe?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37398/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:23:03  来源:igfitidea点击:

How do I make a fully statically linked .exe with Visual Studio Express 2005?

c++visual-studiolinker

提问by Bill Forster

My current preferred C++ environment is the free and largely excellent Microsoft Visual Studio 2005 Express edition. From time to time I have sent release .exe files to other people with pleasing results. However recently I made the disturbing discovery that the pleasing results were based on more luck that I would like. Attempting to run one of these programs on an old (2001 vintage, not scrupulously updated) XP box gave me nothing but a nasty "System cannot run x.exe" (or similar) message.

我目前首选的 C++ 环境是免费且非常出色的 Microsoft Visual Studio 2005 Express 版本。我不时将发布的 .exe 文件发送给其他人,结果令人满意。然而,最近我发现令人不安的结果是基于我想要的更多运气。试图在旧的(2001 年的,没有严格更新的)XP 机器上运行这些程序之一,只给了我一个令人讨厌的“系统无法运行 x.exe”(或类似的)消息。

Some googling revealed that with this toolset, even specifying static linking results in a simple hello-world.exe actually relying on extra .dll files (msvcm80.dll etc.). An incredibly elaborate version scheming system (manifest files anyone?) then will not let the .exe run without exactly the right .dll versions. I don't want or need this stuff, I just want an old fashioned self contained .exe that does nothing but lowest common denominator Win32 operations and runs on any old win32 OS.

一些谷歌搜索显示,使用此工具集,即使指定静态链接也会导致简单的 hello-world.exe 实际上依赖于额外的 .dll 文件(msvcm80.dll 等)。一个令人难以置信的精心设计的版本计划系统(任何人的清单文件?)然后不会让 .exe 在没有完全正确的 .dll 版本的情况下运行。我不想要或不需要这些东西,我只想要一个老式的自包含 .exe,它只执行最低公分母 Win32 操作并在任何旧的 win32 操作系统上运行。

Does anyone know if its possible to do what I want to do with my existing toolset ?

有谁知道是否可以用我现有的工具集做我想做的事?

Thank you.

谢谢你。

回答by Rob Walker

For the C-runtime go to the project settings, choose C/C++ then 'Code Generation'. Change the 'runtime library' setting to 'multithreaded' instead of 'multithreaded dll'.

对于 C 运行时,请转到项目设置,选择 C/C++,然后选择“代码生成”。将“运行时库”设置更改为“多线程”而不是“多线程 dll”。

If you are using any other libraries you may need to tell the linker to ignore the dynamically linked CRT explicitly.

如果您正在使用任何其他库,您可能需要告诉链接器显式忽略动态链接的 CRT。

回答by Sam Buss

My experience in Visual Studio 2010 is that there are two changes needed so as to not need DLL's. From the project property page (right click on the project name in the Solution Explorer window):

我在 Visual Studio 2010 中的经验是,需要进行两项更改才能不需要 DLL。从项目属性页面(右键单击解决方案资源管理器窗口中的项目名称):

  1. Under Configuration Properties --> General, change the "Use of MFC" field to "Use MFC in a Static Library".

  2. Under Configuration Properties --> C/C++ --> Code Generation, change the "Runtime Library" field to "Multi-Threaded (/MT)"

  1. 在配置属性 --> 常规下,将“使用 MFC”字段更改为“在静态库中使用 MFC”。

  2. 在 Configuration Properties --> C/C++ --> Code Generation 下,将“Runtime Library”字段更改为“Multi-Threaded (/MT)”

Not sure why both were needed. I used this to remove a dependency on glut32.dll.

不知道为什么需要两者。我用它来删除对 glut32.dll 的依赖。

Added later: When making these changes to the configurations, you should make them to "All Configurations" --- you can select this at the top of the Properties window. If you make the change to just the Debug configuration, it won't apply to the Release configuration, and vice-versa.

稍后添加:对配置进行这些更改时,您应该将它们设置为“所有配置”——您可以在“属性”窗口的顶部选择此项。如果您仅对 Debug 配置进行更改,它将不适用于 Release 配置,反之亦然。

回答by Jared Updike

I've had this same dependency problem and I also know that you can include the VS 8.0 DLLs (release only! not debug!---and your program has to be release, too) in a folder of the appropriate name, in the parent folder with your .exe:

我遇到了同样的依赖问题,我也知道您可以将 VS 8.0 DLL(仅发布!不能调试!---并且您的程序也必须发布)包含在适当名称的文件夹中,在带有 .exe 的父文件夹:

How to: Deploy using XCopy (MSDN)

如何:使用 XCopy (MSDN) 进行部署

Also note that things are guaranteed to go awry if you need to have C++ and C code in the same statically linked .exe because you will get linker conflicts that can only be resolved by ignoring the correct libXXX.lib and then linking dynamically (DLLs).

另请注意,如果您需要在同一个静态链接的 .exe 中使用 C++ 和 C 代码,事情肯定会出错,因为您会遇到链接器冲突,而这些冲突只能通过忽略正确的 libXXX.lib 然后动态链接(DLL)来解决.

Lastly, with a different toolset (VC++ 6.0) things "just work", since Windows 2000 and above have the correct DLLs installed.

最后,使用不同的工具集 (VC++ 6.0) 可以“正常工作”,因为 Windows 2000 及更高版本安装了正确的 DLL。

回答by bunkerdive

In regards Jared's response, having Windows 2000 or better will not necessarily fix the issue at hand. Rob's response does work, however it is possible that this fix introduces security issues, as Windows updates will not be able to patch applications built as such.

关于 Jared 的回应,拥有 Windows 2000 或更高版本不一定能解决手头的问题。Rob 的响应确实有效,但是此修复程序可能会引入安全问题,因为 Windows 更新将无法修补这样构建的应用程序。

In another post, Nick Guerrera suggests packaging the Visual C++ Runtime Redistributable with your applications, which installs quickly, and is independent of Visual Studio.

在另一篇博文中,Nick Guerrera 建议将 Visual C++ Runtime Redistributable 与您的应用程序一起打包,它可以快速安装并且独立于 Visual Studio。