为什么 #define 抛出“错误:预期声明说明符”?

Jim*_*vid 2 c openmp c-preprocessor c11

我是新人,所以如果我的格式有点不对,我深表歉意。\n我正在编写一个简单的 OpenMP 程序,以便掌握它的窍门,但我已经被一个奇怪的编译完全阻止了错误。我的串行实现编译得很好(使用 gnu11),但我的并行编译似乎由于某种我无法找到的原因而失败。

\n

直到失败点的整个代码如下(make此后我收到的错误如下)

\n
#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\n#include <string.h>\n\n#define N_THR 1\n#define MIN_SIZE 3 //NOTE: min_size is inclusive here\n#ifdef _OPENMP\n    #include <omp.h>\n    #undef N_THR\n    #define N_THR 4\n    #undef MIN_SIZE\n    #define MIN_SIZE N_THR\n    //omp_set_dynamic(false); //we need to explicitly disable dynamic teams to force 4 threads\n    omp_set_num_threads(N_THR);\n#endif\n
Run Code Online (Sandbox Code Playgroud)\n
gcc maze.c -o maze\ngcc maze.c -fopenmp -o mazep\nmaze.c:11:16: error: expected declaration specifiers or \xe2\x80\x98...\xe2\x80\x99 before numeric constant\n  #define N_THR 4\n                ^\nmaze.c:15:22: note: in expansion of macro \xe2\x80\x98N_THR\xe2\x80\x99\n  omp_set_num_threads(N_THR);\n                      ^~~~~\nmake: *** [Makefile:5: parallel] Error 1\n
Run Code Online (Sandbox Code Playgroud)\n

是否有一些我缺少的深层 C 语言语法提示,或者是更明显的东西?

\n

小智 6

这是因为您在omp_set_num_threads()函数外部调用。