已编译 C++可执行文件巨大?

发布于 2024-10-02 08:28:31 字数 302 浏览 6 评论 0原文

在用 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 技术交流群。

扫码二维码加入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.

壹場煙雨 2024-10-09 08:28:32

这更多的是您使用的编译器(和选项)的产物,而不是其他任何东西。使用 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.

千纸鹤带着心事 2024-10-09 08:28:32

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++.

神妖 2024-10-09 08:28:32

这是假 C++ 访谈的一个方面,实际上是真实的:)

您知道,当我们在 AT&T 拥有第一个 C++ 编译器时,我编译了“Hello World”,并且无法相信可执行文件的大小。 2.1MB

什么?嗯,从那时起,编译器已经取得了长足的进步。

他们有吗?在最新版本的 g++ 上尝试一下 - 半兆字节不会带来太大变化。

This is one aspect of the fake C++ interview that is actually kind of true :)

You know, when we had our first C++ compiler, at AT&T, I compiled 'Hello World', and couldn't believe the size of the executable. 2.1MB

What? Well, compilers have come a long way, since then.

They have? Try it on the latest version of g++ - you won't get much change out of half a megabyte.

野味少女 2024-10-09 08:28:31

简而言之,符号

C++ 标准库向您的程序引入了很多符号,因为大多数库主要存在于头文件中。

在发布模式下重新编译您的程序并且不使用调试符号,您可以轻松地预期程序会明显更小。 (如果去掉符号,则仍然更小。)

作为这一事实的快速演示,请观察:

$ cat hello.c
#include <stdio.h>
int main() {
    printf("%s\n", "Hello, world!");
    return 0;
}
$ cat hello.cpp
#include <iostream>
int main() {
    std::cout << "Hello, world!\n";
    return 0;
}
$ gcc hello.c -o hello-c
$ g++ hello.cpp -o hello-cpp
$ gcc hello.c -ggdb -o hello-c-debug
$ g++ hello.cpp -ggdb -o hello-cpp-debug
$ gcc hello.c -s -o hello-c-stripped
$ g++ hello.cpp -s -o hello-cpp-stripped
$ gcc hello.c -s -O3 -o hello-c-stripped-opt
$ g++ hello.cpp -s -O3 -o hello-cpp-stripped-opt
$ ls -gG hello*
-rwxr-xr-x 1  6483 Nov 14 15:39 hello-c*
-rw-r--r-- 1    79 Nov 14 15:38 hello.c
-rwxr-xr-x 1  7859 Nov 14 15:40 hello-c-debug*
-rwxr-xr-x 1  7690 Nov 14 15:39 hello-cpp*
-rw-r--r-- 1    79 Nov 14 15:38 hello.cpp
-rwxr-xr-x 1 19730 Nov 14 15:40 hello-cpp-debug*
-rwxr-xr-x 1  5000 Nov 14 15:45 hello-cpp-stripped*
-rwxr-xr-x 1  4960 Nov 14 15:41 hello-cpp-stripped-opt*
-rwxr-xr-x 1  4216 Nov 14 15:45 hello-c-stripped*
-rwxr-xr-x 1  4224 Nov 14 15:41 hello-c-stripped-opt*

我无法解释为什么使用 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:

$ cat hello.c
#include <stdio.h>
int main() {
    printf("%s\n", "Hello, world!");
    return 0;
}
$ cat hello.cpp
#include <iostream>
int main() {
    std::cout << "Hello, world!\n";
    return 0;
}
$ gcc hello.c -o hello-c
$ g++ hello.cpp -o hello-cpp
$ gcc hello.c -ggdb -o hello-c-debug
$ g++ hello.cpp -ggdb -o hello-cpp-debug
$ gcc hello.c -s -o hello-c-stripped
$ g++ hello.cpp -s -o hello-cpp-stripped
$ gcc hello.c -s -O3 -o hello-c-stripped-opt
$ g++ hello.cpp -s -O3 -o hello-cpp-stripped-opt
$ ls -gG hello*
-rwxr-xr-x 1  6483 Nov 14 15:39 hello-c*
-rw-r--r-- 1    79 Nov 14 15:38 hello.c
-rwxr-xr-x 1  7859 Nov 14 15:40 hello-c-debug*
-rwxr-xr-x 1  7690 Nov 14 15:39 hello-cpp*
-rw-r--r-- 1    79 Nov 14 15:38 hello.cpp
-rwxr-xr-x 1 19730 Nov 14 15:40 hello-cpp-debug*
-rwxr-xr-x 1  5000 Nov 14 15:45 hello-cpp-stripped*
-rwxr-xr-x 1  4960 Nov 14 15:41 hello-cpp-stripped-opt*
-rwxr-xr-x 1  4216 Nov 14 15:45 hello-c-stripped*
-rwxr-xr-x 1  4224 Nov 14 15:41 hello-c-stripped-opt*

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.

离去的眼神 2024-10-09 08:28:31

因为您已经使用 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.

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