C中的#error指令?

时间:2011-03-16 05:59:13

标签: c

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

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

3 个答案:

答案 0 :(得分:32)

当你期望定义几个可能的-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 /* e.g. DEBUG((_debug_trace args)) */
#else
# error "Please specify build type in the Makefile"
#endif

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

答案 1 :(得分:13)

我可能有无效的代码,但它的内容类似于......

#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

答案 2 :(得分:4)

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

#include<stdio.h>
#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