0
点赞
收藏
分享

微信扫一扫

C语言:#error命令,阻止程序编译

#error 指令用于在编译期间产生错误信息,并阻止程序的编译,其形式如下:

#error error_message

例如,我们的程序针对 Linux 编写,不保证兼容 Windows,那么可以这样做:

  1. #ifdef WIN32
  2. #error This programme cannot compile at Windows Platform
  3. #endif

WIN32 是 Windows 下的预定义宏。当用户在 Windows 下编译该程序时,由于定义了 WIN32 这个宏,所以会执行 ​​#error ​​命令,提示用户发生了编译错误,错误信息是:

This programme cannot compile at Windows Platform

需要注意的是:报错信息不需要加引号​​" "​​,如果加上,引号会被一起输出。例如将上面的 #error 命令改为:

#error "This programme cannot compile at Windows Platform"

那么错误信息如下:

C语言:#error命令,阻止程序编译_错误信息


再如,当我们希望以 C++ 的方式来编译程序时,可以这样做:

  1. #ifndef __cplusplus
  2. #error 当前程序必须以C++方式编译
  3. #endif




举报

相关推荐

0 条评论