C中的#error指令?

PHP*_*PHP 35 c

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

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

gee*_*aur 34

它是一个预处理程序指令(例如),当您期望-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
Run Code Online (Sandbox Code Playgroud)

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

  • 说它停止预处理不是更合适吗?我想预处理可以被视为编译中的一个步骤,但它绝对可以作为一个单独的步骤完成,并且在内部作为一个单独的步骤执行,因此它比编译错误更早失败/报告致命错误。 (3认同)

小智 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
Run Code Online (Sandbox Code Playgroud)