有没有办法告诉GCC不要优化一段特定的代码? [英] Is there a way to tell GCC not to optimise a particular piece of code?

查看:787
本文介绍了有没有办法告诉GCC不要优化一段特定的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个依赖于编译器优化的项目,但我需要一些不被GCC优化的代码。这可能吗?

解决方案

GCC从4.4版开始。 #pragma GCC优化(whatever)。我还建议使用 #pragma GCC push_options #pragma GCC pop_options 。第一个选项会保存选项,就像它们在更改之前一样,后面的选项将在之后恢复它们,其余代码将用全局选项进行编译。



有关其他字符串的详细信息,您应该查看gcc doc,这里是其中最重要的部分:参数可以是数字或字符串。数字被认为是一个优化级别。以O开头的字符串被假定为优化选项,而其他选项被假定为与-f前缀一起使用。



这意味着如果你不想对你的特定代码进行任何优化,你的不管应该是0。 b

I am working on a project that relies on compiler optimisations but I need some code not to be optimised by GCC. Is this possible?

解决方案

GCC has since 4.4. the #pragma GCC optimize ("whatever"). I would also recommend to wrap the particular code, that is annotated with this pragma with #pragma GCC push_options and #pragma GCC pop_options. The first will save the options as they were before your change, the later will restore them afterwards and the rest of the code will compile with the global options.

For details on the whatever string, you should look into the gcc doc, here the most important part of it: Arguments can either be numbers or strings. Numbers are assumed to be an optimization level. Strings that begin with O are assumed to be an optimization option, while other options are assumed to be used with a -f prefix..

That means if you dont want any optimizations on your particular code your whatever should just be "0".

这篇关于有没有办法告诉GCC不要优化一段特定的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆