How to reduce the size of executable produced by MinGW g++ compiler?(如何减少 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优化二进制文件的大小.
- -slike you've been doing to strip symbols
- -lstdc++_sto specify dynamically linking against the- libstdc++.dll
- -Osto 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++ 编译器生成的可执行文件的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何减少 MinGW g++ 编译器生成的可执行文件的大小?
 
				
         
 
            
        - 使用/clr 时出现 LNK2022 错误 2022-01-01
- 静态初始化顺序失败 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- C++ 协变模板 2021-01-01
- 从python回调到c++的选项 2022-11-16
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 近似搜索的工作原理 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
 
						 
						 
						 
						 
						 
				 
				 
				 
				