Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended GCC release build flags

Tags:

c++

gcc

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

like image 977
Yasser Asmi Avatar asked Sep 21 '14 21:09

Yasser Asmi


People also ask

Does the order of gcc flags matter?

Order does matter when you use several options of the same kind; for example, if you specify -L more than once, the directories are searched in the order specified. Also, the placement of the -l option is significant.

Which gcc flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.

What are different flags and options with gcc regarding warnings?

You can request many specific warnings with options beginning with ' -W ', for example -Wimplicit to request warnings on implicit declarations. Each of these specific warning options also has a negative form beginning ' -Wno- ' to turn off warnings; for example, -Wno-implicit .


2 Answers

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 (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 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).

like image 181
Mats Petersson Avatar answered Oct 20 '22 12:10

Mats Petersson


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.

like image 41
hookenz Avatar answered Oct 20 '22 13:10

hookenz