I’m trying to do something similar to another question, namely, conditionally include OpenMP pragmas in my program. However, I want to go one step further and avoid that the user needs to specify omp
every time they use the pragma. In other words, I want the following code to compile:
#include <cstdio>
#include <omp.h>
#ifdef _OPENMP
# define LIB_PRAGMA_OMP(x) _Pragma("omp " #x)
#else
# define LIB_PRAGMA_OMP(x)
#endif
int main() {
LIB_PRAGMA_OMP(parallel) {
std::printf("Hello from thread %d\n", omp_get_thread_num());
}
}
Unfortunately, this doesn’t work. The compiler complains:
error:
_Pragma
takes a parenthesized string literal
If I use the following form, it works, though:
#define LIB_PRAGMA_OMP(x) _Pragma(#x)
…
LIB_PRAGMA_OMP(omp parallel) …
However, I’d really like to avoid this redundancy. How can I paste the (stringified) tokens correctly inside the _Pragma
operator?