Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ compiler flag to minimize binary size

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.

*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.

like image 672
Zak Avatar asked Mar 09 '13 18:03

Zak


People also ask

How do I reduce the size of a binary file?

Ultimately, though, the best way to reduce the size of the binary is to remove code and static data from the program. Make it do less, or select programming constructs that result in fewer instructions.

What does the flag to G ++ do?

(Warnings all}) The `Wall' flag tells g++ to turn on all warnings. These warnings frequently indicate possible coding errors that would be difficult to pin down as runtime errors. For this reason, you should always use the `Wall' flag: ``g++ -Wall foo.


2 Answers

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:

  • 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.
like image 85
Tuxdude Avatar answered Sep 24 '22 18:09

Tuxdude


If your code does not contain c++-exception-handling you can save a lot of space (up to 30k after all optimize steps mentioned by Tuxdude). Therefore you have to provide the following flag: -fno-exceptions

But even if you don't use exceptions, the exception handling can be included! Check the following steps:

  1. no usage of new, delete. If you really need it replace them by malloc/free wrappers. For an example search for "tinynew.cpp"

  2. provide function for pure virtual call, e.g.extern "C" void __cxa_pure_virtual() { while(1); }

  3. overwrite __gnu_cxx::__verbose_terminate_handler(). It handles unhandled exceptions and does name demangling, which is quite large! (e.g d_print_comp.part.10 with 9.5k or d_type with 1.8k)

Cheers Flo

like image 43
flozn Avatar answered Sep 23 '22 18:09

flozn