C语言 C中的#error指令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5323349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:04:50  来源:igfitidea点击:

#error directive in C?

c

提问by PHP

Can you please give the information about #errordirective in C?

你能提供有关#errorC指令的信息吗?

What is #errordirective? what the use of it?

什么是#error指令?有什么用呢?

采纳答案by geekosaur

It's a preprocessor directive that is used (for example) when you expect one of several possible -Dsymbols to be defined, but none is.

这是一个预处理器指令,用于(例如)当您希望-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

When the preprocessor hits the #errordirective, it will report the string as an error message and halt compilation; what exactly the error message looks like depends on the compiler.

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

回答by geekosaur

I may have invalid code but its something like...

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

#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

回答by Nishant Kumar

If compiler compiles this line then it shows a compiler fatal error: and stop further compilation of program:

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

#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