How portable is #pragma optimized code?

How portable is code that uses #pragma optimize ? Does most compilers support it, and how full is support for this #pragma ?

+5
source share
5 answers

#pragma is an authorized and portable way for compilers to add unauthorized and non-portable language extensions * .

Basically, you never know for sure, and at least one main C ++ compiler (g ++) does not support this pragma as it is.


*

From the C ++ standard (N3242):

16.6. Pragma Directive [cpp.pragma]

Form preprocessing directive

# pragma pp-tokens opt new line

makes the implementation behave in a certain way. Behavior can lead to a translation error or cause the translator or the resulting program to behave inappropriately. Any pragma that is not recognized by the implementation is ignored.

From standard C (draft Committee - April 12, 2011):

6.10.6 Pragma Directive

Semantics

Form preprocessing directive

# pragma pp-tokens opt new line

where the STDC does not immediately follow pragma in the directive (until replacing any macro) 174) forces the implementation to behave in a certain way. Behavior can lead to a translation error or cause a translator or resulting program to behave inappropriately. Any such pragma that is not recognized by the implementation is ignored.

And here is an example:

 int main () { #pragma omp parallel for for (int i=0; i<16; ++i) {} } 

Most of the OpenMP C and C ++ APIs are implemented as #pragma s.

+11
source

Often you should not rely on compiler flags, as each compiler has its own behavior.

This flag should not be used because it is a compilation layer that you enter into your code.

Usually and theoretically, this flag should be ignored by compilers, if not used.

+6
source

The #pragma keyword is portable in the sense that it should always compile, despite the compiler. However, pragmas are specific to the compiler, so it is likely that when you change the compiler, it will complain about some warnings. Some pragmas are widely used, for example, from OpenMP. To make the code more portable, you can surround your #ifdef / #endif #ifdef , which depend on the compiler used. For instance:

 #ifdef __ICC #pragma optimize #endif 

Compilers usually define some macros, such as __ICC , which make the code know which compiler is being used.

+3
source

#pragma not migrated, full stop. There was a version of gcc that was used to start the game whenever it came across the fact that

Of the compilers that we use at work, two obviously do not support #pragma optimise , and I cannot answer for the others.

And even if they did, because the command line parameters for optimization are different, there is a chance that the parameters for the pragma will be different.

+1
source

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.") 

As for your specific question about #pragma optimize , it is supported by gcc and microsoft , but that does not mean that it will be in the future.

+1
source

All Articles