g++ 编译器标志以最小化二进制大小 [英] g++ compiler flag to minimize binary size

查看:29
本文介绍了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 -Os to make gcc/g++ optimize for size.
  • Use -ffunction-sections -fdata-sections to separate each function or data into distinct sections within the translation unit. Combine it with the linker option -Wl,--gc-sections to get rid of any unreferenced sections.
  • Run strip with at least the following options: -s -R .comment -R .gnu.version. It can be combined with --strip-unneeded to remove all symbols that are not necessary for relocation processing.

这篇关于g++ 编译器标志以最小化二进制大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆