Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build in release mode with optimizations in GCC?

Tags:

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.

like image 427
Polaris878 Avatar asked Oct 08 '09 00:10

Polaris878


People also ask

How do you specify compiler optimizations in GCC?

GCC has a range of optimization levels, plus individual options to enable or disable particular optimizations. The overall compiler optimization level is controlled by the command line option -On, where n is the required optimization level, as follows: -O0 . (default).

How do I enable optimization in GCC?

The -O level option to gcc turns on compiler optimization, when the specified value of level has the following effects: 0. The default reduces compilation time and has the effect that debugging always yields the expected result. This level is equivalent to not specifying the -O option at all.

What compiler option can be used to enable all optimizations?

GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. As compared to -O , this option increases both compilation time and the performance of the generated code.

What is O3 optimization?

Optimization level -O3 -O3 instructs the compiler to optimize for the performance of generated code and disregard the size of the generated code, which might result in an increased code size. It also degrades the debug experience compared to -O2 .


2 Answers

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

like image 144
Josh Avatar answered Nov 02 '22 23:11

Josh


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... 
like image 44
Wernsey Avatar answered Nov 02 '22 23:11

Wernsey