How portable is code that uses #pragma optimize
? Do most compilers support it and how complete is the support for this #pragma
?
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
#pragma
is not portable, full stop. There was a version of gcc that used to start of a game whenever it came across thatOf 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.
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:Compilers usually define some macros such as
__ICC
that make the code know which compiler is being used.#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):
From the C standard (Committee Draft — April 12, 2011):
And here's an example:
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.
Any use of
#pragma
is compiler specific.For example : GNU, Intel and IBM :
Microsoft :
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.