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

1. #error 的用法

(1)#error 是一个预处理器指示字,用于生成一个编译错误消息,这个消息最终会传递到编译器(gcc)

在思考这一点的过程中,领悟到了两个点:

① 使用 gcc 编译代码,输出的错误(警告)信息,是由预处理器、编译器、汇编器、链接器产生的。

② gcc表示整个编译过程,它会调用 预处理器程序 -> 编译器程序 -> 汇编器程序 -> 链接器程序

(2)使用方法:#error message     // 不需要在message上使用双引号

(3)#error 编译指示字用于自定义程序员特有的编译错误消息。类似的,#warning 用于生成编译警告错误

(4)#error 可用于提示编译条件是否满足。编译过程中的任何错误意味着无法生成最终的可执行程序。

下面我们通过一个示例程序来说明 #error 的用法:

下面是一段C++ 的代码,如果我们错误的使用gcc对其进行编译就会报错

 #include <stdio.h>

 class CppClass
{
private:
int m_nValue;
public:
CppClass(){};
~CppClass(){};
}; int main()
{
return ;
}

使用gcc编译该代码报错

那如何解决这个问题呢?答案就是使用 条件编译 + #error 

 #include <stdio.h>

 // __cplusplus宏是C++编译器内置的一个宏,C编译器中是没有的
// 如果使用C编译器编译该程序#error就会报编译报错
#ifndef __cplusplus
#error This file should be processed with C++ compiler
#endif class CppClass
{
private:
int m_nValue;
public:
CppClass(){};
~CppClass(){};
}; int main()
{
return ;
}

在上篇文章的最后,我们分析了一个通过条件编译区分产品版本的小程序,在那个代码中我们没有考虑一种情况,那就是如果没有定义PRODUCT这个宏会怎么样?

#include <stdio.h>

void f()
{
#if (PRODUCT == 1)
printf("This is a low level product!\n");
#elif (PROUDCT == 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. Mannual Service.\n");
printf("6. Exit.\n");
#endif return ;
}

如果我们在编译该程序时没有通过-DPRODUCT指定这个宏的值,编译并不会报错但是执行结果就有问题了。

使用 #error 完善该程序,如果没有定义PRODUCT或者PRODUCT的值不为1、2、3中的一个,程序在编译时就会报错。

 #include <stdio.h>

 void f()
{
#if (PRODUCT == 1)
printf("This is a low level product!\n");
#elif (PROUDCT == 2)
printf("This is a middle level product!\n");
#elif (PRODUCT == 3)
printf("This is a high level product!\n");
#else
// 如果PRODUCT未定义或定义了但!=1 != 2 != 3
#error The PRODUCT macro is NOT defined!
#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. Mannual Service.\n");
printf("6. Exit.\n");
#else
// 如果PRODUCT未定义或定义了但!=1 != 2 != 3
#error The PRODUCT macro is NOT defined!
#endif return ;
}

2. #line 的用法

(1)#line 用于强制指定新的行号和编译文件名,并对源程序的代码重新编号

(2)用法:

#line number newFilename

#line number     // 不改变文件名,只改变行号

(3)#line 编译指示字的本质是重定义 __LINE__ __FILE__

 #include <stdio.h>

 int main()
{
printf("%s : %d\n", __FILE__, __LINE__); #line 1 "new_line.c" // 这里改变了行号和文件名,行号为1(下一行行号为1)、文件名为new_line.c(注意这里需要使用用双引号) printf("%s : %d\n", __FILE__, __LINE__); return ;
} // 输出结果

swj@ubuntu:~/c_course/ch_23$ ./a.out
    line.c : 5
    new_line.c : 2

#line 是C语言早期的产物(在当今的软件工程中已经不使用了),那时候代码量比较小,通常放到一个文件中。如果这个程序由几个人分工协作完成的话,就是每个人先各写各的,最后再统一放到一个文件中。

那如果编译发生错误,如何知道错误的代码是谁写的呢?这个就要使用 #line 预处理指令了。

 #include <stdio.h>

 // The code section is written by A.
// Begin
#line 1 "a.c" // End // The code section is written by B.
// Begin
#line 1 "b.c" // End // The code section is written by Scott.
// Begin
#line 1 "scott_shi.c" int main()
{
printf("%s : %d\n", __FILE__, __LINE__); printf("%s : %d\n", __FILE__, __LINE__) // 这里编译会报错 return ;
} // End

编译报错,提示是 scott_shi.c 这个文件的 第9行 发生错误,这样就定位了是哪个人写的。

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

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

    #error的用法: 示例程序: #include <stdio.h> #ifndef __cplusplus #error This file should be processed w ...

  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. CTF bossplayers 靶机

    WAYs: robots.txt文件提供线索,命令执行漏洞获得反弹shell suid命令提升权限 1:netdiscover 发现主机地址192.168.1.109 2:使用namp进行端口扫描发现 ...

  2. JS的赋值与深浅拷贝实例

    赋值 基本类型: 传值,在栈内存中的数据发生数据变化的时候,系统会自动为新的变量分配一个新的之值在栈内存中,两个变量相互独立,互不影响的 引用类型: 传址,只改变指针的指向,指向同一个对象,两个变量相 ...

  3. SVN的基本使用

    2020年7月6日 为什么需要版本控制? 需要清晰地保存某些文件的不同修订版本 控制文件的发屐过程,找出导致 BUG 的原因 轻松将项目或文件恢复到指定版本 极大方便团队之间协同开发,防止出现混乱 在 ...

  4. [PyTorch 学习笔记] 1.2 Tensor(张量)介绍

    本章代码: https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson1/tensor_introduce1.py https: ...

  5. C++置换的玩笑

    小蒜头又调皮了.这一次,姐姐的实验报告惨遭毒手. 姐姐的实验报告上原本记录着从 1 到 n 的序列,任意两个数字间用空格间隔.但是“坑姐”的蒜头居然把数字间的空格都给删掉了,整个数字序列变成一个长度为 ...

  6. SpringBoot集成Junit

    1.在pom.xml下添加Junit依赖: <!--添加junit环境的jar包--> <dependency> <groupId>org.springframew ...

  7. 图解 K8s 核心概念和术语

    我第一次接触容器编排调度工具是 Docker 自家的 Docker Swarm,主要解决当时公司内部业务项目部署繁琐的问题,我记得当时项目实现容器化之后,花在项目部署运维的时间大大减少了,当时觉得这玩 ...

  8. CentOS7(Linux)源码安装Redis

    介绍 项目中经常需要用到Redis做缓存数据库,可是还有小伙伴不会在Linux上安装Redis,毕竟我们开发的项目都是要在服务器上运行的,今天就来讲讲如何在CentOS7环境使用源码进行安装Redis ...

  9. qt exe文件添加图标

    Qt 怎样生成带图标的exe(转载) 一.问题描述 当我们在 Windows 下用 VS 生成 exe 程序时,如果窗口程序指定了图标,那么生成的 exe 程序便是指定的图标模样. 但是,当使用 Qt ...

  10. 【Flutter 实战】全局点击空白处隐藏键盘

    老孟导读:为什么要实现点击空白处隐藏键盘?因为这是 iOS 平台的默认行为,Android 平台由于其弹出的键盘右上角默认带有关闭键盘的按钮,所以点击空白处不会隐藏键盘. 对于单个页面来说,通过为 T ...