Visual Studio 2010 生成的可执行文件大小较大

发布于 2024-10-20 02:23:00 字数 619 浏览 3 评论 0原文

我有一个最初用 Visual Studio 6.0 编写的 C++ 应用程序

该应用程序是标准的原始 Win32 API,没有 MFC(*Edit 2),没有 .NET,静态链接,多线程可执行文件。

我已经迁移了 2010 年(今天)之前的所有版本的 Visual Studio,直到现在才遇到任何问题:

它可以在 VS2010 上完美编译和运行,但生成的可执行文件大小要大四 (4) 倍!

我已经尝试了所有我知道的选项(优化、删除调试信息等),但没有结果。当然,我是 VS2010 的新手,但不是 Visual Studio 的新手。

有人遇到过这个问题吗?再说一遍:我没有使用任何框架,它是一个原始的、静态链接的 Win32 应用程序,没有 DLL、没有 ODBC、没有网络、没有 .NET

希望再次看到我的可执行文件变小,我感谢您的任何输入。

  • 编辑1: 原始大小=626KB(VS6.0、VS2008) 臃肿的大小=2.013KB (VS2010)

  • 编辑2: 经过一些研究和转储,我发现了对 MFC 的隐藏引用。 本来我说它没有使用 MFC,但它确实使用了。

I've got an C++ application originally written with Visual Studio 6.0

The application is standard and raw Win32 API, no MFC(*Edit 2), no .NET, statically linked, multi-threaded executable.

I have migrated through all versions of Visual Studio up to 2010 (today) and never had any problems until now:

It compiles and runs perfectly with VS2010 BUT the generated executable size is four (4) times larger!

I've tried all the options I know (optimizations, remove debug info, etc..) with no results. Granted, I am new to VS2010, but not to Visual Studio.

Has anyone encountered this problem? Again: I am NOT using any frameworks, it is a raw, statically linked, Win32 application, no DLLs, no ODBC, no network, no .NET

Hoping to see my executables small once again, I thank you for any input.

  • Edit 1:
    Original Size=626KB (VS6.0, VS2008)
    Bloated size=2.013KB (VS2010)

  • Edit 2:
    After some research and dumps, I discovered a hidden reference to MFC.
    Originally I said it did NOT use MFC, but it does.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半步萧音过轻尘 2024-10-27 02:23:00

大小的增加可能是由 MFC 的更改引起的。 这里是一个解释,这里是同一作者的解决方法,它将可执行文件的大小恢复到 2008 年的区域。解决方法涉及编辑 MFC 源文件的副本不过,这个过程可能并不是每个人都满意,并且每次更新后都需要重复该过程,例如安装 Visual Studio 服务包后。

更新:

看起来OP不使用MFC,所以这可能是两个不同的问题。我自己也经历过大小增加,但遗憾的是不能确定是否是由 MFC 引起的,因为我的项目静态链接到 MFC。

The increase in size may be caused by changes to the MFC. Here is an explanation, and here is a workaround by the same author that brings the executable's size back down into regions where it was with 2008. The workaround involves editing copies of MFC source files, though, a process not everybody may be happy with, and which needs to be repeated after every update, e.g. after installing a Visual Studio service pack.

Update:

It looks like the OP does not use MFC, so these might be two different issues. I have experienced the size increase myself but unfortunately cannot say whether caused by MFC or not since my project statically links to the MFC.

酒儿 2024-10-27 02:23:00

如果您使用静态链接,如果您使用以下语法在命令行进行编译,我建议您使用链接器开关:

cl /Ox [您的 C++ 源文件] [需要的库(如果有)] [需要的资源文件(如果有)] /link /FILEALIGN:512 /OPT:REF /OPT:ICF /INCRMENTAL:NO

如果在 Visual Studio IDE 中进行构建,则可以通过从菜单中选择项目属性来检查链接器设置。在配置中,选择发布,然后单击左侧窗格中的链接器设置,这将显示与当前默认设置的链接器设置相对应的配置列表。

在链接器下的命令行中,在“附加选项”条目中指定选项 /FILEALIGN:512,然后单击“应用”按钮。在链接器下的常规选项中,通过选择 No(/INCRMENTAL:NO ) 禁用增量链接。在链接器的调试选项中,为“生成调试信息”选择“否”。对于链接器优化,您可以在“引用”中选择“消除未引用的数据”(/OPT:REF),并在“启用 COMDAT 折叠”中选择“删除冗余 COMDAT”(/OPT:ICF)。

对于编译器优化,请确保选择“Release”配置,单击左侧窗格中的“C/C++”树视图,然后单击“Optimization”,选择“Full Optimization”(/Ox)。在 C/C++ 下的常规设置中,为调试信息格式选择禁用。

不要忘记对您所做的每项更改单击“应用”按钮。

我希望我在这里提到的所有内容都能对您有所帮助,所有这些都适用于 Visual C++ 2005 和 2008,但希望它也适用于 Visual C++ 2010,如果没有,请检查 Visual C++ 2010 安装中包含的文档。

If you are using static linking, I suggest using the linker switches if you are compiling at the command line using the syntax:

cl /Ox [your C++ source file(s)] [libraries required if any] [resource files required if any] /link /FILEALIGN:512 /OPT:REF /OPT:ICF /INCREMENTAL:NO

If you are building within the Visual Studio IDE, you check the linker settings by selecting the project properties from the menu. In the configuration, select the Release and then click the linker settings found on the left pane, this will show you a list of configurations corresponding to the linker settings currently set by default.

In the Command Line under the linker, specify the option /FILEALIGN:512 in the Additional options entry then click the Apply button. In the General option under the linker, disable the incremental linking by selecting No(/INCREMENTAL:NO ). In the debugging option of the linker, select No for Generate Debug Info. For the linker optimization, you select the Eliminate Unreferenced Data(/OPT:REF) in the References and Remove Redundant COMDATs(/OPT:ICF) in the Enable COMDAT folding.

For the compiler optimization, make sure the Release configuration is selected, click the C/C++ tree view on the left pane and under that, click the Optimization, Select Full Optimization(/Ox). In the General setting under C/C++, Select Disabled for the Debug Information Format.

Don't forget to click the Apply button for every changes you make.

I hope everything I've mentioned here would be helpful to you and all of these applies to Visual C++ 2005 and 2008 but hopefully, it would also apply to Visual C++ 2010, if not, kindly check the documentation included with your Visual C++ 2010 installation.

凉宸 2024-10-27 02:23:00

您是否有可能将默认平台目标保留为“任何 CPU”?如果是这样,请将其更改为 x86 以获取仅限 32 位的代码。我猜这将弥补大部分差异。其余的可能是编译器优化的变化(更积极的循环展开以及由于 RAM 便宜而以大小换取速度等)。我相信所有细粒度的优化仍然可以从命令行获得,但许多优化在 UI 选项面板中被遮盖了。

Any chance you've left the default platform target at "Any CPU"? If so, change it to x86 for 32-bit only code. I'm guessing that will make up most of the difference. The remainder is likely in changes in compiler optimizations (more aggressive loop unrolling and whatnot where size has been traded for speed since RAM is cheap). I believe all the granular optimizations are still available from the command line, but many have been obscured in the UI options panels.

浮生面具三千个 2024-10-27 02:23:00

请参阅是否有任何追踪工具C++ 中的膨胀? - 那里描述了一些如何分析影响可执行文件大小的技术。一旦您对 VC 6 和 VS 2010 输出进行分析,希望您会发现一些有用的东西。

从 VC 6 移植到某些 Visual Studio 版本时,有一个特别的问题困扰着我:一些优化选项的含义已更改,并且不再支持我在 VC 6 项目中使用的值,因此生成了 exe by VS 根本没有优化,导致可执行文件膨胀和性能下降。检查 Properties/C/C++/Optimization 中的优化设置,并确保优化位于 /Ox、/O2 或 /O1 上。

See Are there any tools for tracking down bloat in C++? - some techniques how to analyze what contributes to the executable size are described there. Once you perform analysis on both VC 6 and VS 2010 output, hopefully you will find something useful.

There is one particular gotcha which has hit me when porting from VC 6 to some Visual Studio edition: meaning of some optimization options has changed and the values I was using in the VC 6 project were no longer supported, and as a result the exe produced by VS was not optimized at all, causing both executable bloat and slow performance. Check your optimization settings in Properties/C/C++/Optimization and make sure optimization is on /Ox, /O2 or /O1.

本宫微胖 2024-10-27 02:23:00

这是因为在 VS2010 中,微软添加了允许在对话框窗口中使用 HTML 组件的功能,因此即使您不使用它,也会拉入并链接一堆内容,并且优化和选项以及删除未引用的代码没有帮助。没有一种“好的”方法可以删除代码,但有一些破解方法。我们仍然因为这个原因在 VS2008 上编译对大小要求严格的内容。旁注,我们的非 GUI 代码实际上编译得更小,就其价值而言。希望 MS 在修复/补​​丁中提供一个选项,这样我就可以在 VS2010 上做所有事情,但我不会屏住呼吸......

This is because in VS2010 Microsoft added functionality allowing HTML components in a dialog windows so a bunch of stuff is pulled in and linked in even if you aren't using it and the optimizations and options and removing unreferenced code do not help. There is not a "good" way of removing the code, but there are some hacked up ways. We compile our size-critical stuff on VS2008 still because of this. Side note, our non-GUI code actually compiles smaller for what that's worth. Hoping MS comes up with an option for this in a fix/patch so I can do everything on VS2010 but I'm not holding my breath...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文