简体   繁体   English

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

[英]How to disable compiler optimizations in gcc?

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.我已经搜索并找到了如何反汇编.c文件,但我认为它会生成一些优化版本的程序。 Is there any way so that I can see the exact assembly code which corresponds to my C file.有什么方法可以让我看到与我的 C 文件相对应的确切汇编代码。

The gcc option -O enables different levels of optimization. gcc 选项-O启用不同级别的优化。 Use -O0 to disable them and use -S to output assembly.使用-O0禁用它们并使用-S输出程序集。 -O3 is the highest level of optimization. -O3是最高级别的优化。

Starting with gcc 4.8 the optimization level -Og is available.从 gcc 4.8 开始,优化级别-Og可用。 It enables optimizations that do not interfere with debugging and is the recommended default for the standard edit-compile-debug cycle.它启用不干扰调试的优化,并且是标准编辑-编译-调试循环的推荐默认值。

To change the dialect of the assembly to either intel or att use -masm=intel or -masm=att .要将程序集的方言更改为 intel 或 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 手册了解更多信息。

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.对于 gcc,您希望省略任何传递给编译器的-O1 -O2-O3选项,或者如果您已经拥有它们,您可以附加-O0选项以再次将其关闭。 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.它还可以帮助您添加-g进行调试,以便您可以在调试器中查看 c 源代码和反汇编的机器代码。

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

To test without copy elision and see you copy/move constructors/operators in action add "-fno-elide-constructors".要在没有复制省略的情况下进行测试并查看您在操作中复制/移动构造函数/运算符,请添加“-fno-elide-constructors”。

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.即使没有优化 (-O0),GCC 和 Clang 仍然会执行复制省略,这在某些情况下会跳过复制/移动构造函数。 See this question for the details about copy 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 中它确实触发了一个错误(一个没有调用构造函数的无效临时对象),这个错误在 3.5 中得到了修复。

Use the command-line option -O0 (-[capital o][zero]) to disable optimization, and -S to get assembly file.使用命令行选项-O0 (-[capital o][zero]) 禁用优化,使用-S获取程序集文件。 Look here to see more gcc command-line options.查看此处以查看更多 gcc 命令行选项。

Long time ago, but still needed.很久以前,但仍然需要。

info - https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
list - gcc -Q --help=optimizers test.c | grep enabled
disable as many as you like with:
  gcc **-fno-web** -Q --help=optimizers test.c | grep enabled

You can also control optimisations internally with #pragma GCC push_options您还可以使用 #pragma GCC push_options 在内部控制优化

#pragma GCC push_options
/* #pragma GCC optimize ("unroll-loops") */     

.... code here .....

#pragma GCC pop_options

You can disable optimizations if you pass -O0 with the gcc command-line.如果使用 gcc 命令行传递 -O0,则可以禁用优化。

Eg to turn a .C file into a .S file call:例如将 .C 文件转换为 .S 文件调用:

gcc -O0 -S test.c gcc -O0 -S test.c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM