preprocessor error directive --> #error 指令

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

#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 #error directive, it will report the string as an error message and halt compilation; what exactly the error message looks like depends on the compiler.


A preprocessor error directive causes the preprocessor togenerate an error message and causes the compilation to fail.

A #error directive has the form:

             .--------------------.
             V                    |
>>-#--error----preprocessor_token-+----------------------------><
 
 

The argument preprocessor_token is not subject to macro expansion.

The #error directive is often used in the#else portion of a#if-#elif-#elseconstruct, as a safety check during compilation. For example,#error directives in the source file can prevent codegeneration if a section of the program is reached that should bebypassed.

For example, the directive

#define BUFFER_SIZE 255
 
#if BUFFER_SIZE < 256
#error "BUFFER_SIZE is too small."
#endif

generates the error message:

BUFFER_SIZE is too small.



#error

#error "This code should not compile"
The #error macro allows you to make compilation fail and issue a statement that will appear in the list of compilation errors. It is most useful when combined with #if/#elif/#else to fail compilation if some condition is not true. For example:
#ifndef __unix__ // __unix__ is typically defined when targetting Unix 
#error "Only Unix is supported"
#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值