-O0,-O1和-g之间有什么区别?

时间:2012-03-12 08:34:27

标签: c++ c gcc

我想知道使用-O0,-O1和-g来启用lib中的调试符号。 有些人建议使用-O0来启用调试符号,有些人建议使用-g。

那么-g和-O0之间的实际差异是什么?-01和-O0之间的区别是什么,哪个最好用。

5 个答案:

答案 0 :(得分:6)

-O0是优化级别0(没有优化,与省略-O参数相同)

-O1是优化级别1。

-g生成并在二进制文件中嵌入调试符号。

有关详细说明,请参阅gcc docs和联机帮助页。

对于进行实际调试,调试器通常无法理解已经使用优化编译的内容,尽管调试符号即使在优化时也可用于其他事情,例如生成堆栈跟踪。

答案 1 :(得分:4)

-OX指定编译器将执行的optimisation level-g用于生成调试符号。

答案 2 :(得分:2)

来自GCC手册

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.

答案 3 :(得分:1)

-O0不启用调试符号,它只是禁用生成代码中的优化,因此调试更容易(汇编代码或多或少直接遵循C代码)。 -g告诉编译器生成用于调试的符号。

可以为优化代码生成符号(只需继续指定-g),但尝试单步执行代码或设置断点可能无法正常工作,因为发出的代码可能不会“跟随”原始C源密切合作。因此,在这种情况下进行调试会相当棘手。

-O1(与-O相同)执行最小的优化集。 -O0本质上告诉编译器不要优化。有很多选项可以很好地控制编译器执行的方式:http://gcc.gnu.org/onlinedocs/gcc-4.6.3/gcc/Optimize-Options.html#Optimize-Options

答案 4 :(得分:0)

正如其他人所提到的,-O选项集指示编译器必须完成的优化级别,而-g选项添加调试符号。

如需更详细的了解,请参阅以下链接

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