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 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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With