g++ compiler flag to minimize binary size(g ++编译器标志以最小化二进制大小)
问题描述
我有一个 Arduino Uno R3.我正在使用 C++ 为我的每个传感器制作逻辑对象.Arduino 的板载内存非常有限,只有 32KB*,平均而言,我编译的对象大约 6KB*.
I'm have an Arduino Uno R3. I'm making logical objects for each of my sensors using C++. The Arduino has very limited on-board memory 32KB*, and, on average, my compiled objects are coming out around 6KB*.
我已经在使用尽可能少的所需数据类型,以尽量减少我的内存占用.是否有编译器标志来最小化二进制文件的大小,还是我需要使用更短的变量和函数名称、更少的函数等来最小化我的代码库?
I am already using the smallest possible data types required, in an attempt to minimize my memory footprint. Is there a compiler flag to minimize the size of the binary, or do I need to use shorter variable and function names, less functions, etc. to minimize my code base?
此外,任何其他关于最小化二进制大小的提示或建议的话,我们将不胜感激.
Also, any other tips or words of advice for minimizing binary size would be appreciated.
*它可能不是以 KB 为单位衡量的(因为我面前没有它),但 1 个对象大约是我总内存大小的 1/5,这引起了我的担忧.
*It may not be measured in KB (as I don't have it sitting in front of me), but 1 object is approximately 1/5 of my total memory size, which is prompting my concern.
推荐答案
除了评论中提到的 us2012 和其他的,还有很多减少二进制大小的技术,总结一下我自己的观点:
There are lots of techniques to reduce binary size in addition to what us2012 and others mentioned in the comments, summing them up with some points of my own:
- 使用
-Os使 gcc/g++ 优化大小. - 使用
-ffunction-sections -fdata-sections将每个函数或数据分成翻译单元内的不同部分.将其与链接器选项-Wl,--gc-sections结合使用,以删除任何未引用的部分. - 使用至少以下选项运行
strip:-s -R .comment -R .gnu.version.可以和--strip-unneeded结合,去掉所有重定位处理不需要的符号.
- Use
-Osto make gcc/g++ optimize for size. - Use
-ffunction-sections -fdata-sectionsto separate each function or data into distinct sections within the translation unit. Combine it with the linker option-Wl,--gc-sectionsto get rid of any unreferenced sections. - Run
stripwith at least the following options:-s -R .comment -R .gnu.version. It can be combined with--strip-unneededto remove all symbols that are not necessary for relocation processing.
这篇关于g ++编译器标志以最小化二进制大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:g ++编译器标志以最小化二进制大小
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
