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

查看:53
本文介绍了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天全站免登陆