编译C ++可执行文件HUGE? [英] Compiled C++ executables HUGE?

查看:217
本文介绍了编译C ++可执行文件HUGE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C中编程了一段时间后,我决定终于开始学习C ++。这是一种困扰我,因为标准的hello世界在C通常是〜16KB,包括所有的编译器抛出的压力。 (使用stdio)



但是,当我创建一个C ++可执行文件时,hello world,该文件是〜470KB!



我的问题是:
当我包含iostream时,为什么会出现这种情况?我的可执行程序的大小爆炸?



编辑:我使用G ++(使用Dev-CPP IDE,但我可以知道如何添加CL参数) p>

解决方案

总而言之,符号



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



重新编译程序在释放模式和没有调试符号,你可以很容易地期望程序明显更小。



作为这个事实的快速演示,请注意:

  $ cat hello.c 
#include< stdio.h>
int main(){
printf(%s\\\
,Hello,world!
return 0;
}
$ cat hello.cpp
#include< iostream>
int main(){
std :: cout<< Hello,world!\\\
;
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 11月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系统,因此无法测试。


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)

解决方案

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.

这篇关于编译C ++可执行文件HUGE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆