How to decrease the size of generated binaries?

Viewed 25987

I know that there is an option "-Os" to "Optimize for size", but it has little affect, or even increase the size on some occasion :(

strip (or "-s" option) removes debug symbol table, which works fine; but it can only decrease only a small propotion of the size.

Is there any other way to go furthur?

9 Answers

Following code size optimization flags can be used to reduce code size:

  • -Os (both gcc and llvm), -Oz (only LLVM)
  • -fno-unroll-loops
  • -fno-exceptions/-fno-rtti
  • -finline-limit=n (gcc), -mllvm -inline-threshold=n (llvm)
  • -flto, -flto=thin (only LLVM)
  • -fmerge-functions (only LLVM). There might be some bugs in this optimization though.

You might also want to checkout a recent presentation at the Embedded Linux Conference. It presents a comprehensive set of compiler optimizations, and other software engineering techniques to reduce code size.

Link to presentation

slide deck

Disclaimer: I'm the presenter.

I just wanted to add to Chris Eberle's answer that, if you want to learn ARM assembly, here you can also learn how to call C library functions and use dynamic libraries from ARM assembly; and, if you need an ARM virtual machine, you can go here.

Related