How portable is code with #pragma optimize?

2019-05-02 18:32发布

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

5条回答
贪生不怕死
2楼-- · 2019-05-02 19:08

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

查看更多
老娘就宠你
3楼-- · 2019-05-02 19:13

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.

查看更多
一夜七次
4楼-- · 2019-05-02 19:23

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

查看更多
一纸荒年 Trace。
5楼-- · 2019-05-02 19:26

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.

查看更多
爷、活的狠高调
6楼-- · 2019-05-02 19:30

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.

查看更多
登录 后发表回答