Reference:
http://gcc.gnu.org/onlinedocs/gcc/Funct ... agmas.htmlThe following are two examples.
There is a warning message when compiling example 2.
It said this syntax is not supported. But actually, it works. The result is correct.
Example 1: just set optimization level for single function。
__attribute__((optimize("O0")))int add (int a, int b )
{
int x = a;
int y = b;
return x + y;
}
int main ()
{
int r = 1;
int a = r;
int b = r;
func ();
return 0;
}
Example 2:Select partial codes to avoid optimization
Note! The syntax is not only for a function. Users can select arbitrary code segments. #pragma GCC push_options
#pragma GCC optimize ("O0")int add (int a, int b )
{
int x = a;
int y = b;
return x + y;
}
#pragma GCC pop_optionsint main ()
{
int r = 1;
int a = r;
int b = r;
func ();
return 0;
}
#pragma GCC push_options
#pragma GCC pop_options
These two lines represent they will push the original options,
such as -Os.
Then pop the -Os back, to restore the original optimization settings.
Keyword:
optimize, optimization,最佳化,優化