第23课 #error和#line使用分析
#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使用分析的更多相关文章
- 第23课 - #error 和 #line 使用分析
第23课 - #error 和 #line 使用分析 1. #error 的用法 (1)#error 是一个预处理器指示字,用于生成一个编译错误消息,这个消息最终会传递到编译器(gcc) 在思考这一点 ...
- #error和#line使用分析
#error的用法 #error用于生成一个编译错误消息 用法:error message(不需要用双引号包围) #error编译指示字用于自定义程序员特有的编译错误,消息类似的 #warning用于 ...
- 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 ...
- 关于xml加载提示: Error on line 1 of document : 前言中不允许有内容
我是在java中做的相关测试, 首先粘贴下报错: 读取xml配置文件:xmls\property.xml org.dom4j.DocumentException: Error on line 1 of ...
- (转载)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 ...
- 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 ...
- "fatal: protocol error: bad line length character: No This"
git clone 远程地址时候出现 "fatal: protocol error: bad line length character: No This" 错误 在stackov ...
- Parse Fatal Error at line 41 column 24: 元素类型 "url-pattern" 必须由匹配的结束标记 "</url-pattern>" 终止
1.错误描述 严重: Parse Fatal Error at line 41 column 24: 元素类型 "url-pattern" 必须由匹配的结束标记 "< ...
- (转)Windows 平台下解决httpd.exe: syntax error on line 39
近来在研究PHP,结果为了Apache的安装伤神不已...小白我在安装后,启动Apache的服务虽然可以,不过,在Apache sevice monitor 中启动services时就会出现如下的问题 ...
随机推荐
- dubbo-admin与多注册中心(注册中心集群)
在使用dubbo时,注册中心是一个必要的架构组成成员.当我们的注册中心没有采取集群时,如何在dubbo-admin中配置,我们可以根据dubbo官方文档,很快找到我们的答案. 但是当注册中心集群之后怎 ...
- ipvs+ldirectord实现高可用ipvs
一.heartbeat准备 1.接上文 2. 安装heartbeat-ldirectord组件包 [root@node1 heartbeat]# -.el6.x86_64.rpm 3.ldirecto ...
- Hive的三种Join方式
Hive的三种Join方式 hive Hive中就是把Map,Reduce的Join拿过来,通过SQL来表示. 参考链接:https://cwiki.apache.org/confluence/dis ...
- Java实习二
链表(java实现) Link.java public class Link{ private Node first; public Link(){ this.first = null; } //判断 ...
- C# 自动触发鼠标、键盘事件
要在C#程序中触发鼠标.键盘事件必须要调用windows函数. 一.鼠标事件的触发 1.引用windows函数mouse_event /// <summary> /// 鼠标事件 /// ...
- jQuery实际案例④——360导航图片效果
如图:①首先使用弹性盒子布局display:flex; flex-wrap:wrap; ②鼠标移上去出现“百度一下,你就知道了”,这句话之前带上各个网站的logo:③logo使用的是sprite,需要 ...
- vim 正则 捕获
在正规表达式中使用 \( 和 \) 符号括起正规表达式,即可在后面使用\1 \2等变量来访问捕获的内容. 将捕获内容前后交换,如下: :s/\(\haha\)\(hehe\)/\\/
- c++复习要点
自增和自减运算符有前缀和后缀两种形式,都会改变对象,所以不能对常量对象操作. 前缀形式返回改变后的对象,返回*this. 后缀形式返回改变之前的值,所以必须创建一个代表这个值的独立对象并返回它,是通过 ...
- javascript curry 柯里化函数 仿lodash的curry
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Windows下查看占用端口程序
配置shadowsocks,发现本地1080端口被占用,当然更改没有占用的端口即可.当然还得查找下什么程序占用的.无奈,看了下115浏览器占用此端口,浏览器不能管啊,你懂得!!! Windows查找下 ...