有没有办法在宏中用其他语句嵌入pragma语句?
我正在努力实现以下目标:
#define DEFINE_DELETE_OBJECT(type) \
void delete_ ## type_(int handle); \
void delete_ ## type(int handle); \
#pragma weak delete_ ## type_ = delete_ ## type
Run Code Online (Sandbox Code Playgroud)
如果有的话,我可以使用提升解决方案(除了wave).
我正在尝试做一些类似于另一个问题的事情,即在我的程序中有条件地包含OpenMP pragma.但是,我想更进一步,避免用户omp每次使用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());
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不起作用.编译器抱怨:
错误:
_Pragma采用带括号的字符串文字
如果我使用以下表格,它可以工作,但是:
#define LIB_PRAGMA_OMP(x) _Pragma(#x)
…
LIB_PRAGMA_OMP(omp parallel) …
Run Code Online (Sandbox Code Playgroud)
但是,我真的想避免这种冗余.如何在_Pragma操作符内正确粘贴(字符串化)标记?
在我的C++程序中,我想有时使用OpenMP运行其可执行文件,有时不使用OpenMP(即多线程或单线程).我正在考虑以下两种情况中我的代码如何使用OpenMP:
(1)假设我的代码只有#include <omp.h>和OpenMP指令.
(2)与(1)相同,我的代码进一步调用OpenMP函数omp_get_thread_num().
为了不为不同的运行提供不同的代码,是否使用一些自定义的预编译变量来保护OpenMP出现在我的代码中的唯一方法是什么?
感谢致敬!