我有一个简单的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.
请注意,lstdc++_s 标志仅在 GCC > 4.4 的 MinGW 中,我相信.
Note that the lstdc++_s flag is only in MinGW with GCC > 4.4, I believe.
这篇关于如何减少 MinGW g++ 编译器生成的可执行文件的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
fpermissive 标志有什么作用?What does the fpermissive flag do?(fpermissive 标志有什么作用?)
如何在我不想编辑的第 3 方代码中禁用来自 gccHow do you disable the unused variable warnings coming out of gcc in 3rd party code I do not wish to edit?(如何在我不想编辑的第 3 方代码中禁
使用 GCC 预编译头文件Precompiled headers with GCC(使用 GCC 预编译头文件)
如何在 OS X 中包含 omp.h?How to include omp.h in OS X?(如何在 OS X 中包含 omp.h?)
如何让 GCC 将 .text 部分编译为可写在 ELF 二进制文How can I make GCC compile the .text section as writable in an ELF binary?(如何让 GCC 将 .text 部分编译为可写在 ELF 二进制文件中?)
GCC、字符串化和内联 GLSL?GCC, stringification, and inline GLSL?(GCC、字符串化和内联 GLSL?)