GCC C ++“Hello World”程序 - > .exe是500kb大编译时在Windows上。如何减小其大小? [英] GCC C++ "Hello World" program -> .exe is 500kb big when compiled on Windows. How can I reduce its size?

查看:331
本文介绍了GCC C ++“Hello World”程序 - > .exe是500kb大编译时在Windows上。如何减小其大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习C ++ - 我在Windows上使用 nuwen的版本的MingW,使用NetBeans作为IDE(我有在编译这个简单的程序时:


$ b(b)



  #include< iostream> 
using namespace std;

int狗,猫,鸟,鱼;

void f(int pet){
cout< pet id number:<宠物< endl;
}

int main(){
int i,j,k;
cout<< f():<< (长)& f < endl;
cout<< dog:< (长)&狗< endl;
cout<< cat:<< (长)& cat< endl;
cout<< bird:< (长)&鸟< endl;
cout<< fish:< (长)&鱼< endl;
cout<< i:< (长)& i< endl;
cout<< j:<< (长)& j < endl;
cout<< k:< (长)& k < endl;
} ///:〜

我的可执行文件大约是1MB。当我将项目配置从调试更改为发布时,使用-O1 -Os标志(沿着提取调试符号),二进制大小从1MB减少到544KB。 p>

我不是一个大小怪胎,但我只是想知道 - 有什么办法,我可以减少.exe的大小更多?我只是想,这544KB是太多了这么简单的应用程序)。

解决方案

  #include< iostream> 

会导致很多标准库被链接,至少与g ++链接。如果你真的关心可执行文件大小,尝试更换所有使用iostreams与printf或类似。这将通常给你一个更小,更快的可执行文件(我有你的下降到约6K),以方便和类型安全为代价。


I just recently started learning C++ - I am using nuwen's version of MingW on Windows, using NetBeans as an IDE (I have also MSDN AA Version of MSVC 2008, though I don't use it very often).

When compiling this simple program:

#include <iostream>
using namespace std;

int dog, cat, bird, fish;

void f(int pet) {
  cout << "pet id number: " << pet << endl;
}

int main() {
  int i, j, k;
  cout << "f(): " << (long)&f << endl;
  cout << "dog: " << (long)&dog << endl;
  cout << "cat: " << (long)&cat << endl;
  cout << "bird: " << (long)&bird << endl;
  cout << "fish: " << (long)&fish << endl;
  cout << "i: " << (long)&i << endl;
  cout << "j: " << (long)&j << endl;
  cout << "k: " << (long)&k << endl;
} ///:~

my executable was about 1MB big. When I changed project configuration from Debug to Release, used -O1 -Os flags ( stripping debugging symbols along the way ), binary size was reduced from 1MB to 544KB.

I am not a "size freak", but I am just wondering - is there any way, that I could reduce .exe size even more? I just think, that 544KB is just too much for such a simple application ).

解决方案

The

#include <iostream>

causes a lot of the standard library to be linked in, at least with g++. If you are really concerned about executable size, try replacing all uses of iostreams with printf or similar. This will typically give you a smaller, faster executable (I got yours down to about 6K) at the cost of convenience and type-safety.

这篇关于GCC C ++“Hello World”程序 - &gt; .exe是500kb大编译时在Windows上。如何减小其大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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