Issue
I use custom make files for my C++ projects. I am looking for the most recommended compilation flags for release builds. I am currently using the following:
CXXFLAGS += href="https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-O3" rel="nofollow noreferrer">-O3 -Wall -DNDEBUG
I thought the above was sufficient, but then I tried to run the 'strip' tool on the binary, and it shrank the size quite a bit. It looks like there is still some non-essential stuff in the binary.
I know this is a broad topic, but I am looking for common settings for optimal (speed and size) release builds. I know that gcc by default doesn't even discard dead code—I'd like to figure out how to do that soon.
For reference my, the Make setup is on GitHub.
Solution
Yes, stripping after linking will remove global symbols that are included from the C++ library, for example. You can get g++
to do that by using the -s option. It does make debugging the application at a customer site (e.g., telling the customer to run gdb myprog
and then do bt
when it's crashed will not give you any symbols → much harder to find out where the code was, unless you have an identical binary with symbols (or can reproduce one), and you can find the symbols in that (or you can reproduce the problem, but that's wishful thinking a lot of the time)).
If you want small code, you can also use the -Os instead of -O3—that will make the compiler generate optimised code, but not make optimisations that make the code larger (so only inline tiny functions, not unroll loops, etc.). For some cases, small code actually runs faster than "higher optimisation" level but larger code, because medium-sized functions that are called in many places are left as a single function, which is in the cache, rather than being inlined and bloating the application.
Unfortunately it is often hard to say for sure what effect any particular options have on the size of the executable - in some cases, inlining makes for smaller code, other cases it makes the code longer. Unrolling a loop with a count of 2 makes for shorter code than doing the same thing in a loop, etc, etc. If you want fast and small code, you will have to fiddle around with settings and see which ones have what effect on your code. Make sure you keep track of what effect you get from each option. There are quite a few different optimisation options, listed here (that's for 4.9.1, and you can find online versions of older manuals too on the GCC site).
Answered By - Mats Petersson Answer Checked By - Cary Denson (WPSolving Admin)