How portable is code that uses #pragma optimize
? Do most compilers support it and how complete is the support for this #pragma
?
Portability is a characteristic attributed to a computer program if it can be used in an operating systems other than the one in which it was created without requiring major rework. Porting is the task of doing any work necessary to make the computer program run in the new environment.
In general, no, it is not portable in the sense that a compiled library can be linked on an arbitrary other system. The compiled library has to be compatible to the target architecture, the OS, the compiler system, to name some.
C and C++ programs often use low-level features of the underlying system, and therefore are often more difficult to make portable to other platforms. Several standards have been developed to help make your programs more portable.
...is portable. As one of the most frequently used languages in the world and as an open language, C++ has a wide range of compilers that run on many different platforms that support it. Code that exclusively uses C++'s standard library will run on many platforms with few to no changes.
#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.
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