如何禁用gcc中的编译器优化?

时间:2021-07-03 07:04:04

I am trying to learn assembly language. I have searched and found how to disassemble a .c file but I think it produces some optimized version of the program. Is there any way so that I can see the exact assembly code which corresponds to my C file.

我正在努力学习汇编语言。我已经搜索并找到了如何分解一个.c文件,但是我认为它产生了一些优化的程序版本。有什么方法可以让我看到与我的C文件对应的确切的汇编代码吗?

5 个解决方案

#1


74  

The gcc option -O enables different levels of optimization. Use -O0 to disable them and use -S to output assembly. -O3 is the highest level of optimization.

gcc选项-O支持不同级别的优化。使用-O0禁用它们,并将-S用于输出程序集。-O3是最高水平的优化。

Starting with gcc 4.8 the optimization level -Og is available. It enables optimizations that do not interfere with debugging and is the recommended default for the standard edit-compile-debug cycle.

从gcc 4.8开始,优化级别-Og是可用的。它支持不影响调试的优化,并且是标准编辑-编译-调试周期的推荐默认值。

To change the dialect of the assembly to either intel or att use -masm=intel or -masm=att.

将程序集的方言改为英特尔或att使用-masm=intel或-masm=att。

You can also enable certain optimizations manually with -fname.

您还可以使用-fname手动启用某些优化。

Have a look at the gcc manual for much more.

更详细地了解gcc手册。

#2


7  

For gcc you want to omit any -O1 -O2 or -O3 options passed to the compiler or if you already have them you can append the -O0 option to turn it off again. It might also help you to add -g for debug so that you can see the c source and disassembled machine code in your debugger.

对于gcc,您想要忽略传递给编译器的任何-O1 -O2或-O3选项,或者如果您已经有了它们,您可以添加-O0选项来再次关闭它。它还可以帮助您在调试中添加-g,以便在调试器中看到c源代码和分解的机器码。

See also: http://sourceware.org/gdb/onlinedocs/gdb/Optimized-Code.html

参见:http://sourceware.org/gdb/onlinedocs/gdb/Optimized-Code.html

#3


6  

Use the switch -O0 (-[capital o][zero]) to disable optimization, and -S to get assembly file. Look here to see more gcc command line options.

使用switch - o0 (-[capital o][0])来禁用优化,以及- s以获得程序集文件。查看这里可以看到更多的gcc命令行选项。

#4


3  

To test without copy elision and see you copy/move constructors/operators in action add "-fno-elide-constructors".

要测试没有复制的elision,并看到您在操作中复制/移动构造函数/操作符添加“-fno-elide构造函数”。

Even with no optimizations (-O0 ), GCC and Clang will still do copy elision, which has the effect of skipping copy/move constructors in some cases. See this question for the details about copy elision.

即使没有优化(-O0), GCC和Clang仍然会复制elision,这在某些情况下会跳过复制/移动构造函数。有关复制省略的细节,请参阅这个问题。

However, in Clang 3.4 it does trigger a bug (an invalid temporary object without calling constructor), which is fixed in 3.5.

但是,在Clang 3.4中,它确实触发了一个bug(一个无效的临时对象,没有调用构造函数),它是固定在3.5中的。

#5


1  

You can disable optimizations if you pass -O0 with the gcc command-line.

如果使用gcc命令行传递-O0,则可以禁用优化。

E.g. to turn a .C file into a .S file call:

将.C文件转换为.S文件调用:

gcc -O0 -S test.c

gcc o0 - s test.c

#1


74  

The gcc option -O enables different levels of optimization. Use -O0 to disable them and use -S to output assembly. -O3 is the highest level of optimization.

gcc选项-O支持不同级别的优化。使用-O0禁用它们,并将-S用于输出程序集。-O3是最高水平的优化。

Starting with gcc 4.8 the optimization level -Og is available. It enables optimizations that do not interfere with debugging and is the recommended default for the standard edit-compile-debug cycle.

从gcc 4.8开始,优化级别-Og是可用的。它支持不影响调试的优化,并且是标准编辑-编译-调试周期的推荐默认值。

To change the dialect of the assembly to either intel or att use -masm=intel or -masm=att.

将程序集的方言改为英特尔或att使用-masm=intel或-masm=att。

You can also enable certain optimizations manually with -fname.

您还可以使用-fname手动启用某些优化。

Have a look at the gcc manual for much more.

更详细地了解gcc手册。

#2


7  

For gcc you want to omit any -O1 -O2 or -O3 options passed to the compiler or if you already have them you can append the -O0 option to turn it off again. It might also help you to add -g for debug so that you can see the c source and disassembled machine code in your debugger.

对于gcc,您想要忽略传递给编译器的任何-O1 -O2或-O3选项,或者如果您已经有了它们,您可以添加-O0选项来再次关闭它。它还可以帮助您在调试中添加-g,以便在调试器中看到c源代码和分解的机器码。

See also: http://sourceware.org/gdb/onlinedocs/gdb/Optimized-Code.html

参见:http://sourceware.org/gdb/onlinedocs/gdb/Optimized-Code.html

#3


6  

Use the switch -O0 (-[capital o][zero]) to disable optimization, and -S to get assembly file. Look here to see more gcc command line options.

使用switch - o0 (-[capital o][0])来禁用优化,以及- s以获得程序集文件。查看这里可以看到更多的gcc命令行选项。

#4


3  

To test without copy elision and see you copy/move constructors/operators in action add "-fno-elide-constructors".

要测试没有复制的elision,并看到您在操作中复制/移动构造函数/操作符添加“-fno-elide构造函数”。

Even with no optimizations (-O0 ), GCC and Clang will still do copy elision, which has the effect of skipping copy/move constructors in some cases. See this question for the details about copy elision.

即使没有优化(-O0), GCC和Clang仍然会复制elision,这在某些情况下会跳过复制/移动构造函数。有关复制省略的细节,请参阅这个问题。

However, in Clang 3.4 it does trigger a bug (an invalid temporary object without calling constructor), which is fixed in 3.5.

但是,在Clang 3.4中,它确实触发了一个bug(一个无效的临时对象,没有调用构造函数),它是固定在3.5中的。

#5


1  

You can disable optimizations if you pass -O0 with the gcc command-line.

如果使用gcc命令行传递-O0,则可以禁用优化。

E.g. to turn a .C file into a .S file call:

将.C文件转换为.S文件调用:

gcc -O0 -S test.c

gcc o0 - s test.c