确定包含的头文件对总文件大小的贡献
我有兴趣减少我的应用程序的文件大小。它是在 Visual Studio 2008 中使用 MVC++ 构建的 MFC/C++ 应用程序。UPX 很好地将最终的 exe 大小减少到原始大小的 40% 左右,但我想减少更多。
MFC 必须静态链接到该项目中。
我尝试过这个问题中概述的一些方法:reduce-windows-executable-size。特别是对编译器/链接器应用不同的设置。
我相信我可以通过查看项目中包含某些标头的“成本”来进一步减小大小。
有关如何解决此问题的任何提示,也许是一个可以为我分析代码的工具? 谢谢
I am interested in reducing the file size of my application. It is a MFC/C++ application built with MVC++ in Visual Studio 2008. UPX does a good job of reducing the final exe to about 40% of its original size but I would like to reduce it more.
MFC must be statically linked in this project.
I have tried some methods outlined in this question: reduce-windows-executable-size. Specifically applying different settings to the compiler/linker.
I believe i can reduce the size further by having a look at the 'cost' of including certain headers in the project.
Any tip on how to go about this, maybe a tool which could analyse my code for me?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你可能错了。删除标头可能会稍微缩短构建时间,但由于它们包含的内容主要是声明(无论如何您在某些时候都需要它们),因此它们对最终可执行文件的大小影响很小或没有影响。
You are probably wrong in this. Removing headers can result in somewhat shorter build times, but as what they contain is mostly declarations (which you will need at some point anyway) they should have little or no effect on the size of the final executable.
减少项目规模的唯一方法是减少代码量。正如尼尔所说,删除标头只会缩短构建时间。编译器在执行“using namespace”子句时不会包含所有内容,它们只会选择需要的内容。现在,另一方面,如果您向项目中根本没有使用的东西添加标头,那么这很好地表明了应该删除的类。
The only way to reduce the size of the project is to reduce the amount of code. As Neil stated, removing headers will only shorten the build time. Compilers will not include everything when doing a "using namespace" clause, they'll only select that which is needed. Now, on the other hand, if you're adding headers to things not used anywhere at all within the project that is a good indication of a class that should be removed.
您的假设显然是可执行文件大小在某种程度上是贡献组件(特别是源文件)的总和。事实并非如此。
例如,假设代码为
std::list::size
。它可用于许多翻译单元。然而,链接器会将许多副本折叠在一起,有时甚至是不同类型的 T。但是您如何解释可执行文件中生成的字节?现在,如果您甚至无法确定如何解释该简单(一组)函数的字节,那么您将如何解释更复杂的构造?如果您无法将可执行文件中使用的字节分配给各个源文件,那么您就无法确定各个贡献。
Your assumption apparently is that executable size is somehow the sum of contributing components, and in particular source files. It just doesn't work that way.
For instance, assume the code for
std::list<T>::size
. It may be used in many Translation Units. Yet, the linker will fold many copies together, sometimes even for different types T. But how would you account for the resulting bytes in the exectuable?Now, if you can't even determine how to account for the bytes of that simple (set of) functions, then how would you account for even more complex constructs? And if you can't allocate bytes used in the executable to indivual source files, then you can't determine individual contributions.
标头通常只包含:
上述任何内容都不会真正生成机器代码,除非它们实际被您的代码中使用。 .c/.cpp 文件。
现在以上所有内容都需要解析(这会增加编译时间),但除非实际使用,否则将被忽略。
Headers typically only contain:
None of the above actually results in machine code being generated unless they are actually used by the code in your .c /.cpp files.
Now all of the above needs to be parsed (which adds to compile time) but will be ignored unless actually used.
大多数现有答案都假设标头仅包含声明,这对可执行文件的大小没有影响。当然,如果该假设成立,那就是真的,但是标头也包含实际代码(通常是函数定义)变得相当普遍,并且这些确实会影响代码大小。
另一方面,只有当这些函数实际上链接到最终的可执行文件中时,它们才会做出贡献。仅当您调用这些函数时,它们才会链接到最终的可执行文件。因此,即使标头可能会增加可执行文件的大小,您打算怎么做呢?您无法删除您使用的代码。那么删除头文件就不是一个选择——除非您将该代码移到其他地方,然后它会增加可执行文件的大小无论如何。
因此,标头要么没有什么区别,要么确实有区别,因为您使用了其中的代码,然后它就无法删除。无论哪种情况,删除标头都不会给你带来什么好处。
Most of the existing answers have assumed that headers only contain declarations, which have no impact on executable size. Of course that's true if that assumption holds, but it is getting fairly common for headers to contain actual code as well (function definitions, typically), and those do contribute to code size.
On the other hand, they only contribute if those functions are actually linked into the final executable. And they are only linked into the final executable if you call those functions. So even though the header might contribute to growing your executable size, what are you going to do about it? You can't remove the code that you use. Removing the header file just isn't an option then -- unless you move that code somewhere else, and then it'll increase the executable size anyway.
So either a header makes no difference, or it does make a difference because you use the code that's in it, and then it can't be removed. In either case, removing headers isn't going to buy you much.