Search code examples
visual-c++portabilitypragmac-preprocessor

_Pragma preprocessor operator in Visual C++


Is there something like the ANSI C operator _Pragma in Visual C++?

For example, I'm trying to define the following macro:

#ifdef _OPENMP
#define PRAGMA_IF_OPENMP(x) _Pragma (#x)
#else  // #ifdef _OPENMP
#define PRAGMA_IF_OPENMP(x)
#endif  // #ifdef _OPENMP

So I can circumvent compiler warnings for unknown #pragma omp ... in older GCC compilers. Is there a similar means available in VisualC++?


Solution

  • Yes, but it's two underscores: __pragma

    I'm not sure about how the omp pragma works, however, here's an example using VC++'s optimize pragma:

    #define PRAGMA_OPTIMIZE_OFF __pragma(optimize("", off))
    
    // These two lines are equivalent
    #pragma optimize("", off)
    PRAGMA_OPTIMIZE_OFF
    

    EDIT: I've just confirmed that the omp pragmas can also be used like this:

    #define OMP_PARALLEL_FOR __pragma(omp parallel for)
    

    So, yes, your macro should work if defined as follows (note that your original code incorrectly used the stringizing operator #x:

    #ifdef _OPENMP
    #define PRAGMA_IF_OPENMP(x) __pragma (x)
    #else  // #ifdef _OPENMP
    #define PRAGMA_IF_OPENMP(x)
    #endif  // #ifdef _OPENMP