Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How portable is code with #pragma optimize?

How portable is code that uses #pragma optimize? Do most compilers support it and how complete is the support for this #pragma?

like image 996
sharptooth Avatar asked Nov 07 '12 10:11

sharptooth


People also ask

What does it mean for a code to be portable?

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.

Is compiled code portable?

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.

Is C++ code portable?

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.

Why is C++ considered 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.


1 Answers

#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-line

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 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-line

where the preprocessing token STDC does not immediately follow pragma 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 such pragma 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 #pragmas.

like image 80
Sebastian Mach Avatar answered Oct 25 '22 07:10

Sebastian Mach