How portable is code that uses #pragma optimize
? Do most compilers support it and how complete is the support for this #pragma
?
#pragma
is the sanctioned and portable way for compilers to add non-sanctioned and non-portable language extensions *.
Basically, you never know for sure, and at least one major C++ compiler (g++) does not support this pragma as is.
*:
From the C++ standard (N3242):
16.6 Pragma directive [cpp.pragma]
A preprocessing directive of the form
# pragma
pp-tokensopt new-linecauses the implementation to behave in an implementation-defined manner. The behavior might cause translation to fail or cause the translator or the resulting program to behave in a non-conforming manner. Any pragma that is not recognized by the implementation is ignored.
From the C standard (Committee Draft — April 12, 2011):
6.10.6 Pragma directive
Semantics
A preprocessing directive of the form
# pragma
pp-tokensopt new-linewhere the preprocessing token
STDC
does not immediately followpragma
in the directive (prior to any macro replacement)174) causes the implementation to behave in an implementation-defined manner. The behavior might cause translation to fail or cause the translator or the resulting program to behave in a non-conforming manner. Any suchpragma
that is not recognized by the implementation is ignored.
And here's an example:
int main () {
#pragma omp parallel for
for (int i=0; i<16; ++i) {}
}
A big part of the C and C++ OpenMP API is implemented as #pragma
s.
Often this is not a good idea to rely on compiler flags, since each compiler has its own behaviour.
This flag should not be used as it is a compiling level spec you inject into your code.
Normally and theoretically this flag should be ignored by compilers if not used.
The #pragma
keyword is portable in the sense that it should always compile despite on the compiler. However, the pragmas are compiler-specific so it's probable that when changing compiler it will complain with some warnings. Some pragmas are wide used, such as these from OpenMP. In order to make the code the most portable possible, you might surround your pragmas with #ifdef
/#endif
that depend on the compiler you're using. For example:
#ifdef __ICC
#pragma optimize
#endif
Compilers usually define some macros such as __ICC
that make the code know which compiler is being used.
Any use of #pragma
is compiler specific.
For example : GNU, Intel and IBM :
#warning "Do not use ABC, which is deprecated. Use XYZ instead."
Microsoft :
#pragma message("Do not use ABC, which is deprecated. Use XYZ instead.")
Regarding your specific question about the #pragma optimize
, it is supported by gcc and microsoft, but it doesn't mean it will be in the future.
#pragma
is not portable, full stop. There was a version of gcc that used to start of a game whenever it came across that
Of the compilers we use at work, two definitely don't support #pragma optimise
, and I can't answer for the others.
And even if they did, as the command line switches for optimisation are different, the chances are that the options for the pragma would be different.
来源:https://stackoverflow.com/questions/13267648/how-portable-is-code-with-pragma-optimize