I am developing a C++ for ARM using GCC. I have ran into an issue where, I no optimizations are enabled, I am unable to create a binary (ELF) for my code because it will not fit in the available space. However, if I simply enable optimization for debugging (-Og), which is the lowest optimization available to my knowledge, the code easily fits.
In both cases, -ffunction-sections, -fdata-sections, -fno-exceptions, and -Wl,--gc-sections is enabled.
This is huge difference in binary size even with minimal optimizations.
I took a look at 3.11 Options That Control Optimization for details on to what optimizations are being performed with the -Og flag to see if that would give me any insight.
What optimization flags affect binary size the most? Is there anything I should be looking for to explain this massive difference?
Which GCC optimization flags affect binary size the most?
It will vary somewhat depending on the program itself. Most accurate way to find out how each flag affects your program is to try it out and compare the result with base-level.
A good choice of base-level for size optimisation is to use -Os which enables all optimisations of -O2 except those that are expected to potentially increase the binary size significantly which are (at the moment):
-falign-functions
-falign-jumps
-falign-labels
-falign-loops
-fprefetch-loop-arrays
-freorder-blocks-algorithm=stc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With