Visual C++中的_Pragma预处理程序运算符

Man*_*uel 6 portability pragma visual-c++ c-preprocessor

是否有像_PragmaVisual C++中的ANSI C运算符?

例如,我正在尝试定义以下宏:

#ifdef _OPENMP
#define PRAGMA_IF_OPENMP(x) _Pragma (#x)
#else  // #ifdef _OPENMP
#define PRAGMA_IF_OPENMP(x)
#endif  // #ifdef _OPENMP
Run Code Online (Sandbox Code Playgroud)

因此,我可以绕过#pragma omp ...旧GCC编译器中未知的编译器警告.VisualC++中是否有类似的方法?

Bla*_*way 7

是的,但这是两个下划线: __pragma

我不确定omppragma 是如何工作的,但是,这是一个使用VC++的optimizepragma 的例子:

#define PRAGMA_OPTIMIZE_OFF __pragma(optimize("", off))

// These two lines are equivalent
#pragma optimize("", off)
PRAGMA_OPTIMIZE_OFF
Run Code Online (Sandbox Code Playgroud)

编辑:我刚刚确认omppragma也可以像这样使用:

#define OMP_PARALLEL_FOR __pragma(omp parallel for)
Run Code Online (Sandbox Code Playgroud)

所以,是的,如果定义如下,你的宏应该工作(注意你的原始代码错误地使用了字符串化操作符#x:

#ifdef _OPENMP
#define PRAGMA_IF_OPENMP(x) __pragma (x)
#else  // #ifdef _OPENMP
#define PRAGMA_IF_OPENMP(x)
#endif  // #ifdef _OPENMP
Run Code Online (Sandbox Code Playgroud)

  • @Manuel - 我认为你把`_Pragma`与'__pragma`混淆了; 不幸的是,它们具有不同语法的不同实体(后者是MSVC特定的).请参阅:http://stackoverflow.com/questions/3030099/cc-pragma-in-define-macro/3030312#3030312 (5认同)