8

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++?

Manuel
  • 6,461
  • 7
  • 40
  • 54
  • 1
    I recommend just disabling that warning. OpenMP pragmas are designed to be otherwise harmless in compiler that does not support them. – Jan Hudec Apr 24 '14 at 16:58

1 Answers1

7

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
Blair Holloway
  • 15,969
  • 2
  • 29
  • 28
  • Actually, the GCC documentation gives an example with `#x`. – Manuel Jan 25 '11 at 17:03
  • 5
    @Manuel - I think you're confusing `_Pragma` with `__pragma`; they are unfortunately different entities with different syntaxes (and the latter is MSVC specific). See: http://stackoverflow.com/questions/3030099/c-c-pragma-in-define-macro/3030312#3030312 – Blair Holloway Jan 26 '11 at 00:50