5
我試圖做類似於another question的事情,即在我的程序中有條件地包含OpenMP pragmas。但是,我想更進一步,避免用戶每次使用附註時需要指定omp
。換句話說,我想要下面的代碼來編譯:在GCC的_Pragma運算符中粘貼預處理器令牌
#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());
}
}
不幸的是,這是行不通的。編譯器會抱怨:
error:
_Pragma
takes a parenthesized string literal
如果我用下面的形式,它的工作原理,但:
#define LIB_PRAGMA_OMP(x) _Pragma(#x)
…
LIB_PRAGMA_OMP(omp parallel) …
不過,我真的想避免這種冗餘。 如何在_Pragma
運算符中正確粘貼(字符串化的)令牌?
夥計,如此明顯。 :) –
如果它是如此明顯,爲什麼它像我試過的第20件事? :) –