如何减少 MinGW g++ 编译器生成的可执行文件的大小? [英] How to reduce the size of executable produced by MinGW g++ compiler?

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

问题描述

我有一个简单的Hello world"C++ 程序,它在 Win XP 下由 MinGW g++ 编译器编译为 500kB 可执行文件.有人说是iostream库和libstdc++.dll的静态链接造成的.

I have a trivial "Hello world" C++ program that is compiled to 500kB executable by MinGW g++ compiler under Win XP. Some say that is caused by iostream library and static link of libstdc++.dll.

使用 -s 链接器选项有所帮助(减少了 50% 的大小),但我只对 <10kB 可执行文件感到满意.有什么方法可以使用 MinGW 编译器实现这一点吗?便携性对我来说不是一个大问题.

Using -s linker option helped a bit (reducing 50% size), but I would by satisfied only by <10kB executable. Is there any way how to achieve this using MinGW compiler? Portability is not a big concern for me.

是否可以使用动态链接将 libstdc++.dll 与可执行文件一起复制?如果是这样,如何实现这一目标?

Is it possible to copy libstdc++.dll with the executable using dynamic linking? If so, how to achieve this?

已解决:我使用的是 MinGW 3.4.现在我更新到最新的 MinGW 4.6,大小减少了 90% 到 50kB,使用 -s 选项甚至到 9kB,这完全足够了.无论如何 - 感谢大家的帮助.这是我的结果

Solved: I was using MinGW 3.4. Now I updated to latest MinGW 4.6 and the size was decreased by 90% to 50kB, with -s option even to 9kB, which is fully sufficient. Anyway - thanks everyone for help. Here you go my results

C++ Hello World 使用 iostream 的程序

C++ Hello World program using iostream

MinGW | no options | -s option
------------------------------
3.4   | 500kB      | 286 kB
4.6   | 50kB       |   9 kB

推荐答案

要使用的标志:

  • -s 就像你一直在做的去除符号
  • -lstdc++_s 指定动态链接到 libstdc++.dll
  • -Os 优化二进制文件的大小.
  • -s like you've been doing to strip symbols
  • -lstdc++_s to specify dynamically linking against the libstdc++.dll
  • -Os to optimize the binary for size.

默认情况下,mingw 静态链接到 Windows 上的 libstdc++.a.

By default mingw static links to libstdc++.a on Windows.

请注意,lstd​​c++_s 标志仅在 GCC > 4.4 的 MinGW 中,我相信.

Note that the lstdc++_s flag is only in MinGW with GCC > 4.4, I believe.

这篇关于如何减少 MinGW g++ 编译器生成的可执行文件的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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