简体   繁体   中英

Recommended GCC release build flags

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 += -O3 -Wall -DNDEBUG

I thought the above was sufficient, but then I tried to run '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 optimizal (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 make setup is https://github.com/YasserAsmi/buildmk

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 customer site (eg 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 setitngs 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, you can find online versions of older manuals too on the gcc site).

CXXFLAGS += -O3 -Wall -DNDEBUG

Is fine. Assuming NDEBUG is used, I never use that personally. If you need a smaller binary, use the "-s" strip symbol table which is similar to what strip would do. Generally I don't bother and leave the symbol table there as it can still be useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM