如何减少 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 thelibstdc++.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.
相关文章