tags:

views:

542

answers:

4

Hello, What are the specific options I would need to build in "release mode" with full optimizations in GCC? If there are more than one option, please list them all. Thanks.

+5  A: 

http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

There is no 'one size fits all' - you need to understand your application, your requirements and the optimisation flags to determine the correct subset for your binary.

Or the answer you want: -O3

Josh
I did and couldn't find a straight forward explanation. I could just suck at searching though.
Polaris878
Besides, the point of SO is to show up in search engines like Google, so its better to have it here anyways.
Polaris878
Fair call.Is this what you were after? Or do you need a bit more context/info?
Josh
Often, you'll want `-DNDEBUG` too to strip the assertions out.
Martin B
+2  A: 

-O2 will turn on all optimizations that don't require a space\speed trade off and tends to be the one I see used most often. -O3 does some space for speed trade offs(like function inline.) -Os does O2 plus does other things to reduce code size. This can make things faster than O3 by improving cache use. (test to find out if it works for you.) Note there are a large number of options that none of the O switches touch. The reason they are left out is because it often depends on what kind of code you are writing or are very architecture dependent.

stonemetal
+2  A: 

Note that gcc doesn't have a "release mode" and a "debug mode" like MSVC does. All code is just code. The presence of the various optimization options (-O2 and -Os are the only ones you generally need to care about unless you're doing very fine tuning) modifies the generated code, but not in a way to prevent interoperability with other ABI-compliant code. Generally you want optimization on stuff you want to release.

The presence of the "-g" option will cause extended symbol and source code information to be placed in the generated files, which is useful for debugging but increases the size of the file (and reveals your source code), which is something you often don't want in "released" binaries.

But they're not exclusive. You can have a binary compiled with optimization and debug info, or one with neither.

Andy Ross
Optimisation should usually be disabled if you intend to debug the generated code though, otherwise you'll find debugging to be an experience akin to a David Lynch film.
caf
It's not that bad, I look at stack dumps and valgrind output from "release" binaries all the time. As long as you don't use -fomit-stack-pointer and understand that statics might be inlined and invisible, you can read an awful lot of context out of even a heavily optimized binary. If your functions are short you can usually figure out the expression that crashed with a guess or two.
Andy Ross
Unlike a David Lynch film, though, debuggers let the viewer toggle at will between the different levels of reality. If you understand disassembly, then single-stepping through that (with the source lines interspersed) usually makes a certain amount of sense even with optimisation. If you don't understand the disassembly, then you'll learn.
Steve Jessop
On a related note, there are ways to keep the debug symbols for maintenance in "Release" builds. One way to do it: `gcc -g source.cc -o a.out; strip a.out -o a.out.syms; strip a.out`. Use `objdump -t a.out` to verify the symbols have actually been stripped.
kizzx2
+3  A: 

Here is a part from a Makefile that I use regularly (in this example, it's trying to build a program named foo).

If you run it like $ make BUILD=debug or $ make debug then the Debug CFLAGS will be used. These turn off optimization (-O0) and includes debugging symbols (-g).

If you omit these flags (by running $ make without any additional parameters), you'll build the Release CFLAGS version where optimization is turned on (-O2), debugging symbols stripped (-s) and assertions disabled (-DNDEBUG).

As others have suggested, you can experiment with different -O* settings dependig on your specific needs.

ifeq ($(BUILD),debug)   
# "Debug" build - no optimization, and debugging symbols
CFLAGS += -O0 -g
else
# "Release" build - optimization, and no debug symbols
CFLAGS += -O2 -s -DNDEBUG
endif

all: foo

debug:
    make "BUILD=debug"

foo: foo.o
    # The rest of the makefile comes here...
iWerner