开发者

Is there a way to tell GCC not to optimise a particular piece of code?

I am working on a project that relie开发者_JS百科s on compiler optimisations but I need some code not to be optimised by GCC. Is this possible?


GCC 4.4 has an attribute for that:

int foo(int i) __attribute__((optimize("-O3")));

It is documented at: https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/Function-Attributes.html#index-g_t_0040code_007boptimize_007d-function-attribute-3195


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".


You can put that piece of code into a different file and compile it without optimisations.

Or try to use the #pragma directive:

#pragma optimize level=0 

Or even better start and stop optimization with :

#pragma OPTIMIZE ON 
#pragma OPTIMIZE OFF
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜