C中的#error指令?

你能否在C中提供有关#error指令的信息?

什么是#error指令? 它的用途是什么?

当你期望定义几个可能的-D符号之一时,它是一个预处理器指令(例如),但没有一个。

 #if defined(BUILD_TYPE_NORMAL) # define DEBUG(x) do {;} while (0) /* paranoid-style null code */ #elif defined(BUILD_TYPE_DEBUG) # define DEBUG(x) _debug_trace x /* eg DEBUG((_debug_trace args)) */ #else # error "Please specify build type in the Makefile" #endif 

当预处理器命中#error指令时,它会将该字符串报告为错误消息并停止编译; 错误消息的确切含义取决于编译器。

我可能有无效的代码但它的东西像……

 #if defined USING_SQLITE && defined USING_MYSQL #error You cannot use both sqlite and mysql at the same time #endif #if !(defined USING_SQLITE && defined USING_MYSQL) #error You must use either sqlite or mysql #endif #ifdef USING_SQLITE //... #endif #ifdef USING_MYSQL //... #endif 

如果编译器编译此行,则它显示编译器致命错误:并停止进一步编译程序:

 #include #ifndef __MATH_H #error First include then compile #else int main(){ float a,b=25; a=sqrt(b); printf("%f",a); return 0; } #endif Output:compiler error --> Error directive :First include then compile