已编译 C++可执行文件巨大?
在用 C 编程了一段时间后,我终于决定开始学习 C++。这有点困扰我,因为 C 中的标准“hello world”通常约为 16KB,包括编译器抛出的所有内容。 (使用 stdio)
但是,当我创建一个执行 hello world 的 C++ 可执行文件时,该文件约为 470KB!我继续使用 cstdio 而不是 iostream,认为这会有所作为,而且确实如此。
我的问题是: 当我包含 iostream 时,为什么我的可执行文件的大小会爆炸?
编辑:我正在使用 G++(使用 Dev-CPP IDE,但我可以弄清楚如何添加 CL 参数)
After programming for a while in C, I decided to finally start to learn C++. This is sort of bothering me, as the standard 'hello world' in C is usually ~16KB, including all of the crud your compiler throws on there. (Using stdio)
However, when I create a C++ executable doing hello world, the file is ~470KB! I went ahead and used cstdio instead of iostream, thinking it would make a difference and it did.
My question is:
When I include iostream, why does the size of my executable explode?
Edit: I'm using G++ (With the Dev-CPP IDE, but I can figure out how to add CL paramaters)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我猜想,通过包含
,您会间接包含 STL 的许多部分,例如
,而
又包含<;矢量>
等I would guess that by including
<iostream>
you're indirectly including many parts of the STL, such as<string>
which in turn includes<vector>
etc.这更多的是您使用的编译器(和选项)的产物,而不是其他任何东西。使用 MS VC++,根据我使用的编译器标志,我可以获得从 ~8K 到 ~110K 的任何值。使用 MinGW,我得到大约 24-25K(同样,取决于标志)。
以防万一您想知道,我猜我使用 VC++ 获得的更大范围主要是更好地了解其标志的结果。 MinGW可能只覆盖较小的范围,即使我更了解它,但由于我对其标志的了解有限,我接受它默认情况下的大部分功能;我知道如何打开和关闭优化,但必须非常仔细地看待事情才能做更多的事情。
This is more an artifact of the compiler (and options) you use than almost anything else. With MS VC++, depending on the compiler flags I use I can get anywhere from ~8K to ~110K. Using MinGW, I get around 24-25K (again, depending on flags).
Just in case you're wondering, I'd guess the larger range I get with VC++ is mostly a result of knowing its flags better. MinGW might only cover a smaller range even if I knew it better, but due to my limited knowledge of its flags I'm accepting most of what it does by default; I know how to turn optimization on and off, but have to look at things pretty carefully to do a lot more than that.
MinGW (g++) 编译非常大的文件。
例如,使用 iostreams 的相同“hello world”程序通过 VC++(使用静态链接的 CRT)编译为约 100KB,通过 g++ 编译为约 470KB。
MinGW (g++) compiles really big files.
For example the same "hello world" program with iostreams compiles to ~100KB by VC++ (with statically linked CRT) and to ~470KB by g++.
这是假 C++ 访谈的一个方面,实际上是真实的:)
This is one aspect of the fake C++ interview that is actually kind of true :)
简而言之,符号。
C++ 标准库向您的程序引入了很多符号,因为大多数库主要存在于头文件中。
在发布模式下重新编译您的程序并且不使用调试符号,您可以轻松地预期程序会明显更小。 (如果去掉符号,则仍然更小。)
作为这一事实的快速演示,请观察:
我无法解释为什么使用 G++ 的 Windows 构建程序会产生如此大的可执行文件,但在任何其他平台上,符号是主要驱动因素在大文件大小中。我目前无法访问Windows系统,所以无法测试。
In a word, symbols.
The C++ standard library introduces a lot of symbols to your program, since most of the library exists primarily in the header files.
Recompile your program in release mode and without debug symbols, and you can easily expect the program to be significantly smaller. (Smaller still if you strip symbols.)
As a quick demonstration of this fact, observe:
I can't explain why a Windows build of the programs with G++ produces such large executables, but on any other platform, symbols are the primary driving factor in large file sizes. I don't have access to a Windows system at the moment, so I can't test.
因为您已经使用 iostream 拖入了大部分标准库。
不过,这是一次性的事情,因此随着程序变得越来越大,它的开销似乎会越来越小。
但是,您可能希望使用标准库的共享库版本进行编译,并且大多数编译器/操作系统将允许您执行此操作,因此您不必在可执行文件中包含所有标准库。您使用的是哪种编译器,我们可能会建议您如何执行此操作。
例如,在具有 VC 命令行的 Windows 上,使用 /MD 命令行选项。
Becuase you've dragged in most of the standard library by using iostreams.
It's a one off thing though so as your programs gets larger it will seem less and less of an overhead.
However you probably want to compile using a shared library version of the standard library and most compilers / operating systems will let you do this so you'll not have to include all of the standard library in your executable. Which compiler are you using and we can likely advise on how to do that.
On windows with VC command line, use the /MD command line option for example.