781 字
4 分钟
[ICS大作业] 第二章 预处理
2.1 预处理的概念和作用
预处理是编译过程的第一步,主要负责处理源代码中的预处理指令,如宏定义、文件包含和条件编译等。预处理器会将这些指令转换为相应的代码片段,生成一个中间文件(通常以.i为后缀),该文件不包含任何预处理指令,只有纯粹的C代码
预处理的主要作用包括:
- 宏替换:将源代码中的宏定义替换为相应的代码片段
- 文件包含:处理
#include指令,将指定的头文件内容插入到源代码中 - 条件编译:根据条件编译指令(如
#ifdef、#ifndef、#endif等)选择性地编译代码块 - 生成调试信息:预处理器可以生成调试信息,帮助开发者定位代码中的问题
2.2 预处理命令
cpp hello.c -o hello.i
2.3 Hello的预处理结果解析
因生成文件过大无法贴出全部内容,以下为部分内容解析
# 0 "hello.c"# 0 "<built-in>"# 0 "<命令行>"# 1 "/usr/include/stdc-predef.h" 1 3 4# 0 "<命令行>" 2# 1 "hello.c"
...# 0 "hello.c": 表示接下来的代码来自hello.c文件的第 0 行# 0 "<built-in>": 表示接下来的代码来自编译器内置代码的第 0 行# 0 "<命令行>": 表示接下来的代码来自命令行参数(宏定义)的第 0 行# 1 "/usr/include/stdc-predef.h" 1 3 4: 表示接下来的代码来自/usr/include/stdc-predef.h文件的第 1 行,标志1表示文件包含开始,标志3表示这是系统头文件# 0 "<命令行>" 2: 表示接下来的代码回到命令行参数的第 0 行,标志2表示文件包含结束# 1 "hello.c": 表示接下来的代码来自hello.c文件的第 1 行
# 1 "/usr/include/stdio.h" 1 3 4# 28 "/usr/include/stdio.h" 3 4# 1 "/usr/include/bits/libc-header-start.h" 1 3 4# 33 "/usr/include/bits/libc-header-start.h" 3 4# 1 "/usr/include/features.h" 1 3 4# 415 "/usr/include/features.h" 3 4# 1 "/usr/include/features-time64.h" 1 3 4# 20 "/usr/include/features-time64.h" 3 4# 1 "/usr/include/bits/wordsize.h" 1 3 4# 21 "/usr/include/features-time64.h" 2 3 4# 1 "/usr/include/bits/timesize.h" 1 3 4# 19 "/usr/include/bits/timesize.h" 3 4# 1 "/usr/include/bits/wordsize.h" 1 3 4# 20 "/usr/include/bits/timesize.h" 2 3 4
...可见此部分用于包含hello.c中通过#include指令包含的各类头文件,如stdio.h,以及stdio.h中包含的其他头文件
而后续大量代码即为这些头文件的内容,包含各种函数声明、宏定义和类型定义等
翻到文件末尾,可以看到hello.c中的代码:
# 10 "hello.c" 2
# 11 "hello.c"int main(int argc,char *argv[]){ int i;
if(argc!=5){ printf("用法: Hello 学号 姓名 手机号 秒数!\n"); exit(1); } for(i=0;i<10;i++){ printf("Hello %s %s %s\n",argv[1],argv[2],argv[3]); sleep(atoi(argv[4])); } getchar(); return 0;}这部分代码与hello.c中的代码完全一致,说明预处理器已经完成了宏替换和文件包含等工作
2.4 本章小结
本章介绍了预处理的概念和作用,讲解了如何使用预处理命令生成预处理结果,并对hello.c的预处理结果进行了大致解析,理解了预处理器如何处理源代码中的预处理指令
[ICS大作业] 第二章 预处理
https://a1kari8.github.io/posts/ics/preprocess/