【发布时间】:2010-01-23 22:15:37
【问题描述】:
是否有 GCC pragma 指令可以停止、暂停或中止编译过程?
我使用的是 GCC 4.1,但我希望编译指示也可以在 GCC 3.x 版本中使用。
【问题讨论】:
-
如果您告诉我们您希望停止编译的原因,我们可能会提供更好的答案。
-
对 GCC 3-4.1 的限制是否仍然相关?
标签: gcc
是否有 GCC pragma 指令可以停止、暂停或中止编译过程?
我使用的是 GCC 4.1,但我希望编译指示也可以在 GCC 3.x 版本中使用。
【问题讨论】:
标签: gcc
你可能想要#error:
$ cd /tmp
$ g++ -Wall -DGoOn -o stopthis stopthis.cpp
$ ./stopthis
Hello, world
$ g++ -Wall -o stopthis stopthis.cpp
stopthis.cpp:7:6: error: #error I had enough
#include <iostream>
int main(void) {
std::cout << "Hello, world\n";
#ifndef GoOn
#error I had enough
#endif
return 0;
}
【讨论】:
#error 不能在宏内使用,尽管这个问题的目的很模糊。
我不知道#pragma,但#error 应该做你想做的事:
#error Failing compilation
它将终止编译并显示错误消息“编译失败”。
【讨论】:
这行得通:
#include <stophere>
GCC 在找不到包含文件时停止。如果不支持 C++14,我希望 GCC 停止。
#if __cplusplus<201300L
#error need g++14
#include <stophere>
#endif
【讨论】:
虽然通常#error 就足够了(并且可移植),但有时您想使用pragma,即,当您希望有选择地在宏中导致错误时。
这是一个使用示例,它依赖于 C11's _Generic 和 _Pragma。
此示例确保在编译时var 不是int * 或short *,但不是const int *。
例子:
#define MACRO(var) do { \
(void)_Generic(var, \
int *: 0, \
short *: 0, \
const int *: 0 _Pragma("GCC error \"const not allowed\"")); \
\
MACRO_BODY(var); \
} while (0)
【讨论】:
#pragma GCC error "error message"
参考:7 Pragmas
【讨论】:
你可以使用:
#pragma GCC error "my message"
但这不是标准的。
【讨论】: