2

Possible Duplicates:
C/C++ pragma in define macro
Conditional “pragma omp”

How can I use an OpenMP pragmas inside a macro definition? E.g.

#define A() { \
 ...a lot of code... \
 #pragma omp for     \
 for(..)             \
  ..do_for..         \
 ...another a lot of code \
 }
Community
  • 1
  • 1
osgx
  • 90,338
  • 53
  • 357
  • 513

3 Answers3

3

As it was answered here Conditional "pragma omp"

C99 has the _Pragma keyword that allows you to place what otherwise would be #pragma inside macros. Something like

  #define OMP_PARA_INTERNAL _Pragma("omp parallel for")

So,

#define OMP_PARA_FOR _Pragma("omp for")
#define A() { \
 ...a lot of code... \
 OMP_PARA_FOR     \
 for(..)             \
  ..do_for..         \
 ...another a lot of code \
 }

several _Pragmas can be combined as unary operator, e.g.:

_Pragma("first pragma") _Pragma("Second pragma")
Community
  • 1
  • 1
osgx
  • 90,338
  • 53
  • 357
  • 513
2

On Visual Studio you can use the __pragma keyword instead of #pragma. Unfortunately, this is non-standard so you'll have to figure out whether your compiler supports something similar, if you're using another compiler.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
1

You can't because # inside a macro has another meaning (to add quotes (#somevar) or concat variables (onevar ## anothervar)). If you've got more complex code you'd like to include in-place instead of a function call, use a local function and add the inline keyword so it's inlined just as a macro would be inlined. You just have to ensure you pass variables used inside as parameters (as pointers or references). As an alternative you could use #include to include a code file containing your macro code only.

Mario
  • 35,726
  • 5
  • 62
  • 78