_Pragma preprocessor operator in Visual C++

女生的网名这么多〃 提交于 2019-11-28 03:55:56

问题


Is there something like the ANSI C operator _Pragma in Visual C++?

For example, I'm trying to define the following macro:

#ifdef _OPENMP
#define PRAGMA_IF_OPENMP(x) _Pragma (#x)
#else  // #ifdef _OPENMP
#define PRAGMA_IF_OPENMP(x)
#endif  // #ifdef _OPENMP

So I can circumvent compiler warnings for unknown #pragma omp ... in older GCC compilers. Is there a similar means available in VisualC++?


回答1:


Yes, but it's two underscores: __pragma

I'm not sure about how the omp pragma works, however, here's an example using VC++'s optimize pragma:

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

// These two lines are equivalent
#pragma optimize("", off)
PRAGMA_OPTIMIZE_OFF

EDIT: I've just confirmed that the omp pragmas can also be used like this:

#define OMP_PARALLEL_FOR __pragma(omp parallel for)

So, yes, your macro should work if defined as follows (note that your original code incorrectly used the stringizing operator #x:

#ifdef _OPENMP
#define PRAGMA_IF_OPENMP(x) __pragma (x)
#else  // #ifdef _OPENMP
#define PRAGMA_IF_OPENMP(x)
#endif  // #ifdef _OPENMP


来源:https://stackoverflow.com/questions/4782049/pragma-preprocessor-operator-in-visual-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!