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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5765899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:28:40  来源:igfitidea点击:

How to disable compiler optimizations in gcc?

cgcccompiler-optimizationcompiler-options

提问by Neal

I am trying to learn assembly language. I have searched and found how to disassemble a .cfile 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 文件相对应的确切汇编代码。

回答by pmr

The gcc option -Oenables different levels of optimization. Use -O0to disable them and use -Sto output assembly. -O3is the highest level of optimization.

gcc 选项-O启用不同级别的优化。使用-O0他们,使用禁用-S输出组件。-O3是最高级别的优化。

Starting with gcc 4.8 the optimization level -Ogis 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=intelor -masm=att.

要将程序集的方言更改为 intel 或 att 使用-masm=intel-masm=att.

You can also enable certain optimizations manually with -fname.

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

Have a look at the gcc manualfor much more.

查看gcc 手册了解更多信息。

回答by IanNorton

For gcc you want to omit any -O1 -O2or -O3options passed to the compiler or if you already have them you can append the -O0option to turn it off again. It might also help you to add -gfor 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

回答by user3770818

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. See this questionfor the details about copy elision.

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

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 中得到了修复。

回答by MByD

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

使用命令行选项-O0(-[capital o][zero]) 禁用优化并-S获取程序集文件。查看此处以查看更多 gcc 命令行选项。

回答by api

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

回答by Nils Pipenbrinck

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

回答by mckenzm

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