#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. 如何使一个openwrt下的软件开机自启动

    条件有三: 1.需要在软件包的Makefile中添加宏定义Package/$(package-name)/preinst和Package/$(package-name)/prerm define Pa ...

  2. 值得推荐的10本PHP书籍(转)

    值得推荐的10本PHP书籍(转) 一.总结 一句话总结: 二.值得推荐的10本PHP书籍 本篇文章的目的是想较全面地推荐10本PHP书籍,暂不讨论Linux/NGINX/Mysql等其他丛书. 前言 ...

  3. winmm.dll获取和设置声音

    [DllImport("winmm.dll")] private static extern int waveOutGetVolume(IntPtr hwo, out uint d ...

  4. 使用python编写微信跳一跳的自动脚本

    实现思路: 调用adb命令,截图 寻找小小人的底部中心点role(从下到上扫描,直到找到小小人相同像素的点,至于小小人像素点rgb是什么,可以使用photoshop查看) 寻找棋盘最高点top,然后寻 ...

  5. Codeforces Round #412

    第一题水题,8分钟1a #include<map> #include<set> #include<cmath> #include<queue> #inc ...

  6. js判断回车,判断焦点控件

    document.onkeydown=function(event){        e = event ? event :(window.event ? window.event : null);  ...

  7. nyoj——113 getline

    字符串替换 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 编写一个程序实现将字符串中的所有"you"替换成"we"   输入 ...

  8. OC与JS的交互详解

    事情的起因还是因为项目需求驱动.折腾了两天,由于之前没有UIWebView与JS交互的经历,并且觉得这次在功能上有一定的创造性,特此留下一点文字,方便日后回顾. 我要实现这样一个需求:按照本地的CSS ...

  9. zk的watcher机制的实现

    转载:https://www.ibm.com/developerworks/cn/opensource/os-cn-apache-zookeeper-watcher/ http://blog.csdn ...

  10. vue踩坑之旅 -- computed watch

    vue踩坑之旅 -- computed watch 经常在使用vue初始化组件时,会报一些莫名其妙的错误,或者,数据明明有数据,确还是拿不到,这是多么痛苦而又令人忍不住抓耳挠腮,捶胸顿足啊 技术点 v ...