#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. 如果css足够强大了,你还会用编程的方式实现去实现css已有的功能吗?

    现在css3 都出来的,但是其实我由于一些原因,有些css2中都能支持的样式,我都没有使用过.我感觉我真的有必要静下心来,去看看那些东西,看看哪些以前都被忽视掉的. 今天我主要来讲三个对于我们编程经常 ...

  2. 快速升级openwrt的linux内核版本

    一.分析 要升级openwrt的linux内核版本,关键是要制作内核配置文件 二.内核配置文件制作方法 2.1当前openwrt对应的某个开发板有对应的内核配置文件,比如此时的openwrt的linu ...

  3. linux下安装sphinx

    1.下载sphinx源码包 上面截图的这个网址   复制链接地址   在putty终端使用:wget http://sphinxsearch.com/files/sphinx-2.3.1-beta.t ...

  4. 管理账号密码的工具-KeePass使用方法

    附件链接:https://files.cnblogs.com/files/stxs/KeePass.zip 打开压缩包“KeePass.zip",将文件"KeePass.exe&q ...

  5. LVS-net

    一.LVS基本情况 lvs:Linux Virtual Server,是一种负载均衡集群,其主要是由工作在内核的ipvs与用户空间的命令行工具ipvsadm组成.支持TCP,UDP,AH,EST,AH ...

  6. .Net Core Linux部署之进程守护 Supervisor 安装配置

    1.Supervisor 安装 //安装easy_install yum install python-setuptools //安装Supervisor easy_install superviso ...

  7. Qwt中鼠标获取坐标点

    void getPoint(QwtPlot *plot) { QPoint point = plot->canvas()->mapFromGlobal(QCursor::pos()); Q ...

  8. 我的Ansible学习笔记

    Ansible常见错误 http://afewbug.com/article/26官方文档 http://docs.ansible.com/ansible/安装配置 http://sofar.blog ...

  9. OpenID 和 OAuth 的区别及第三方登录的安全隐患分析

    转自:http://itindex.net/detail/48552-openid-oauth-%E6%96%B9%E7%99%BB 发表时间:2014-03-13 19:09 | 作者:天梯梦 出处 ...

  10. HDU - 5988The 2016 ACM-ICPC Asia Qingdao Regional ContestG - Coding Contest 最小费用流

    很巧妙的建边方式 题意:有n个区域,每个区域有一些人数si和食物bi,区域之间有m条定向路径,每条路径有人数通过上限ci.路径之间铺了电线,每当有人通过路径时有pi的概率会触碰到电线,但是第一个通过的 ...