Dir*_*tel 48
你可能想要#error:
edd@ron:/tmp$ g++ -Wall -DGoOn -o stopthis stopthis.cpp
edd@ron:/tmp$ ./stopthis
Hello, world
edd@ron:/tmp$ g++ -Wall -o stopthis stopthis.cpp
stopthis.cpp:7:6: error: #error I had enough
edd@ron:/tmp$ cat stopthis.cpp
#include <iostream>
int main(void) {
std::cout << "Hello, world\n";
#ifndef GoOn
#error I had enough
#endif
return 0;
}
edd@ron:/tmp$
Run Code Online (Sandbox Code Playgroud)
Mic*_*ael 16
我不知道a #pragma,但#error应该做你想做的事:
#error Failing compilation
Run Code Online (Sandbox Code Playgroud)
将使用错误消息"Failing compilation"终止编译
虽然通常#error足够(并且可移植),但有时您想要使用a pragma,即,当您想要在宏中导致错误时.
这是一个示例用途,它取决于C11 _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)
Run Code Online (Sandbox Code Playgroud)
这有效:
#include <stophere>
Run Code Online (Sandbox Code Playgroud)
gcc在找不到包含文件时停止.如果不支持C14,我希望gcc停止.
#if __cplusplus<201300L
#error need g++14
#include <stophere>
#endif
Run Code Online (Sandbox Code Playgroud)