哪些修改会导致 C++ 代码中二进制大小的减小 [英] What modifications will lead to size reduction of binary size in C++ code
问题描述
我一直在研究非常大的 C++ 项目代码库.大约有 2k 个文件,包含大约 200k 行代码.代码包括大量使用模板代码中有很多内联当前使用带有 O2 选项的 clang++ 进行编译最终的可执行文件大小约为 50 MB
I have been working on very big code base of C++ project. There are about 2k files and comprising about 200k lines of code. The code includes heavy usage of templates There is lot of inlining in code Currently using clang++ with O2 option for compiling The final executable size is about 50 MB
出于某种原因,我想进一步减小二进制大小.已经采取的步骤1> 尽可能用非模板代码替换模板2> 将 XML 库从 xerces 替换为 expat
For some reason, I want to reduce the binary size still further. Steps already taken 1> Replace templates with non-template code where possible 2> Replace XML library from xerces to expat
欢迎在这方面提出任何建议
Any suggestions in this regard are welcome
推荐答案
以下是常用的减小程序大小的方法,
The following methods are commonly used to reduce the size of programs,
使用特定于您的编译器的技术来减小大小.
Use your compiler specific Techniques to reduce the size.
使用gcc -S program.c
编译得到汇编文件.您现在可以执行基于汇编程序的空间优化.
Compile using gcc -S program.c
to get the Assembler file. You can now perform assembler based space optimizations.
减少 C 中全局变量的数量.
Reduce the number of global variables in C.
使用简单的算法,而不是给您带来非常小的执行时间变化的复杂算法.例如,如果列表中的元素数量不是很大,则使用 bubble sort
而不是 Merge sort
.
Instead of complex algorithms which gives you very small changes in the execution time, use simple algorithms. For example use bubble sort
instead of Merge sort
if the number of elements in the list is not very large.
删除只使用一两次的简单函数.
Remove simple functions which are used just once or twice.
消除死代码.许多通常在大型项目中都有.
Eliminate dead code. Many often in large projects there are some.
注意您在程序中包含的库函数.
Be careful about the library functions you include in your program.
这篇关于哪些修改会导致 C++ 代码中二进制大小的减小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!