【发布时间】:2011-09-30 09:09:59
【问题描述】:
我正在尝试做类似于another question 的事情,即有条件地在我的程序中包含 OpenMP 编译指示。但是,我想更进一步,避免用户每次使用 pragma 时都需要指定 omp。换句话说,我想编译下面的代码:
#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());
}
}
不幸的是,这不起作用。编译器抱怨:
错误:
_Pragma采用带括号的字符串文字
如果我使用以下表格,它可以工作,但是:
#define LIB_PRAGMA_OMP(x) _Pragma(#x)
…
LIB_PRAGMA_OMP(omp parallel) …
但是,我真的很想避免这种冗余。 如何在_Pragma 运算符中正确粘贴(字符串化)标记?
【问题讨论】:
标签: c++ gcc c-preprocessor