dead programmer
dead programmer

Reputation: 4373

What is the difference between -O0 ,-O1 and -g

I am wondering about the use of -O0,-O1 and -g for enabling debug symbols in a lib. Some suggest to use -O0 to enable debug symbols and some suggest to use -g.

So what is the actual difference between -g and -O0 and what is the difference between -01 and -O0 and which is best to use.

Upvotes: 5

Views: 10528

Answers (5)

Sanish
Sanish

Reputation: 1729

From GCC manual

http://gcc.gnu.org/onlinedocs/

3.10 Options That Control Optimization`

-O

-O1

Optimize. Optimizing compilation takes somewhat more time, and a lot more memory for a large function. With -O, the compiler tries to reduce code size and execution time, without performing any optimizations that take a great deal of compilation time.`

-O2

Optimize even more. 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.`

-O3

Optimize yet more. -O3 turns on all optimizations specified by -O2 and also turns on the -finline-functions, -funswitch-loops, -fpredictive-commoning, -fgcse-after-reload, -ftree-vectorize and -fipa-cp-clone options.`

-O0

Reduce compilation time and make debugging produce the expected results. This is the default. `

-g

Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF 2). GDB can work with this debugging information.`

Upvotes: 4

Michael Burr
Michael Burr

Reputation: 340506

-O0 doesn't enable debug symbols, it just disables optimizations in the generated code so debugging is easier (the assembly code follows the C code more or less directly). -g tells the compiler to produce symbols for debugging.

It's possible to generate symbols for optimized code (just continue to specify -g), but trying to step through code or set breakpoints may not work as you expect because the emitted code will likely not "follow along" with the original C source closely. So debugging in that situation can be considerably trickier.

-O1 (which is the same as -O) performs a minimal set of optimizations. -O0 essentially tells the compiler not to optimize. There are a slew of options that allow a very fine control over how you might want the compiler to perform: http://gcc.gnu.org/onlinedocs/gcc-4.6.3/gcc/Optimize-Options.html#Optimize-Options

Upvotes: 3

Jay
Jay

Reputation: 24905

As mentioned by others, -O set of options indicate the levels of optimization that must be done by the compiler whereas, the -g option adds the debugging symbols.

For a more detailed understanding, please refert to the following links

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

Upvotes: 0

nos
nos

Reputation: 229342

-O0 is optimization level 0 (no optimization, same as omitting the -O argument)

-O1 is optimization level 1.

-g generates and embeds debugging symbols in the binaries.

See the gcc docs and manpages for further explanation.

For doing actual debugging, debuggers are usually not able to make sense of stuff that's been compiled with optimization, though debug symbols are useful for other things even with optimization, such as generating a stacktrace.

Upvotes: 11

MByD
MByD

Reputation: 137442

-OX specify the optimisation level that the compiler will perform. -g is used to generate debug symbols.

Upvotes: 5

Related Questions