#error的用法:

示例程序:

 #include <stdio.h>

 #ifndef __cplusplus
#error This file should be processed with C++ compiler.
#endif class CppClass
{
private:
int m_value;
public:
CppClass()
{ } ~CppClass()
{
}
}; int main()
{
return ;
}

先注释掉3-5行的代码,使用gcc编译结果如下:

当出现这样的编译错误后不一定是我们的代码错了,可能是我们的编译器用错了。比如我们使用了开源代码或者第三方库经常会出现这样的错误。这样的错误我们不好定位,甚至看不懂这样的错误信息,这时我们只需要加上第3-5行的代码,然后再进行编译。

加上3-5行的程序后,gcc编译结果如下:

#error在条件编译中的应用:

 #include <stdio.h>

 void f()
{
#if ( PRODUCT == 1 )
printf("This is a low level product!\n");
#elif ( PRODUCT == 2 )
printf("This is a middle level product!\n");
#elif ( PRODUCT == 3 )
printf("This is a high level product!\n");
#endif
} int main()
{
f(); printf("1. Query Information.\n");
printf("2. Record Information.\n");
printf("3. Delete Information.\n"); #if ( PRODUCT == 1 )
printf("4. Exit.\n");
#elif ( PRODUCT == 2 )
printf("4. High Level Query.\n");
printf("5. Exit.\n");
#elif ( PRODUCT == 3 )
printf("4. High Level Query.\n");
printf("5. Mannul Service.\n");
printf("6. Exit.\n");
#endif return ;
}

我们在编译的时候如果忘记了定义PRODUCT宏,也可以编译通过,但运行结果不是我们想要的,我们希望在没有定义PRODUCT宏的时候,程序编译报错。

我们修改程序,加入#error编译错误信息:

当我们不定义PRODUCT宏直接编译时,结果如下:

这正是我们想要的结果。

#line的用法

#line在现代编译器中基本不再使用了。

使用示例:

以前的程序由多人开发,而且都写在一个文件中,出现编译错误时不知道是谁的程序出错了,所以引入了#line。

小结:

第23课 #error和#line使用分析的更多相关文章

  1. 第23课 - #error 和 #line 使用分析

    第23课 - #error 和 #line 使用分析 1. #error 的用法 (1)#error 是一个预处理器指示字,用于生成一个编译错误消息,这个消息最终会传递到编译器(gcc) 在思考这一点 ...

  2. #error和#line使用分析

    #error的用法 #error用于生成一个编译错误消息 用法:error message(不需要用双引号包围) #error编译指示字用于自定义程序员特有的编译错误,消息类似的 #warning用于 ...

  3. Error on line -1 of document : Premature end of file. Nested exception: Premature end of file.

    启动tomcat, 出现, ( 之前都是好好的... ) [lk ] ERROR [08-12 15:10:02] [main] org.springframework.web.context.Con ...

  4. 关于xml加载提示: Error on line 1 of document : 前言中不允许有内容

    我是在java中做的相关测试, 首先粘贴下报错: 读取xml配置文件:xmls\property.xml org.dom4j.DocumentException: Error on line 1 of ...

  5. (转载)Flash Loader加载完成不发送COMPLETE和ERROR事件的问题分析

    (转载)http://blog.dou.li/flash-loader%E5%8A%A0%E8%BD%BD%E5%AE%8C%E6%88%90%E4%B8%8D%E5%8F%91%E9%80%81co ...

  6. error on line 1 at column 6: XML declaration allowed only at the start of the document

    This page contains the following errors: error on line 1 at column 6: XML declaration allowed only a ...

  7. "fatal: protocol error: bad line length character: No This"

    git clone 远程地址时候出现 "fatal: protocol error: bad line length character: No This" 错误 在stackov ...

  8. Parse Fatal Error at line 41 column 24: 元素类型 "url-pattern" 必须由匹配的结束标记 "</url-pattern>" 终止

    1.错误描述 严重: Parse Fatal Error at line 41 column 24: 元素类型 "url-pattern" 必须由匹配的结束标记 "< ...

  9. (转)Windows 平台下解决httpd.exe: syntax error on line 39

    近来在研究PHP,结果为了Apache的安装伤神不已...小白我在安装后,启动Apache的服务虽然可以,不过,在Apache sevice monitor 中启动services时就会出现如下的问题 ...

随机推荐

  1. 高并发下,php与redis实现的抢购、秒杀功能

    抢购.秒杀是如今很常见的一个应用场景,主要需要解决的问题有两个: 1 高并发对数据库产生的压力 2 竞争状态下如何解决库存的正确减少("超卖"问题) 对于第一个问题,已经很容易想到 ...

  2. 数据结构实习 problem L 由二叉树的中序层序重建二叉树

    由二叉树的中序层序重建二叉树 writer:pprp 用层序中序来重建二叉树 代码点这里 其实本质上与前序中序建立二叉树没有什么太大区别 大概思路: 递归解法,对当前层进行处理,通过层序遍历可以得到当 ...

  3. [C# 开发技巧系列]如何动态设置屏幕分辨率

    首先,大家应该明确,现在没有可用的API来给我们动态地设置屏幕分辨率,我们要实现这个需求,我们只能在C#程序中调用Win32 API 函数来解决这个问题的,这里用C#代码调用Win32 API 就涉及 ...

  4. php调用mysql存储过程

    MYSQL存储过程 原文链接:http://blog.sina.com.cn/s/blog_52d20fbf0100ofd5.html 一.存储过程简介(mysql5.0以上支持) 我们常用的操作数据 ...

  5. python+opencv+pyqt5控制摄像头在Qlabel上显示

    import cv2 import numpy as numpy from PIL import * import sys from PyQt5.QtWidgets import * from PyQ ...

  6. 深入理解AUC

    https://tracholar.github.io/machine-learning/2018/01/26/auc.html 我觉得作者写的很不错

  7. python:使用Fabric自动化你的任务

    http://www.th7.cn/Program/Python/2012/03/05/62236.shtml

  8. Android开发--List与ArrayList区别

    List是一个接口,而ArrayList是一个类.  ArrayList继承并实现了List.  所以List不能被构造,但可以向上面那样为List创建一个引用,而ArrayList就可以被构造.  ...

  9. css 权威指南笔记

    部分属性选择: 选择class 属性中包含warning的元素 [class~="warning"]{font-weight:bold} 子串匹配属性选择器: 在现代浏览器中得到支 ...

  10. 【转】VIM 中设置Tab

    灵活操作 Vim 中的 Tabsgaleki post @ 2007年11月16日 05:07PM in Vim Tips with tags: vim tabs Vim 支持 Tabs,也就是标签页 ...