C语言进阶剖析 19 编译过程简介

被隐藏了的事实


初识编译器

在这里插入图片描述

你不知道的事

在这里插入图片描述

编译器做了什么

预处理器

  • 处理所有的注释,以空格代替
  • 将所有的 #define 删除,并且展开所有的宏定义
  • 处理条件编译指令 #if, #ifdef, #elif, #else, #endif
  • 处理 #inlcude ,展开被包含的文件
  • 保留编译器需要使用的 #pargma 指令
        ■ 预处理指令示例: gcc -E file.c -o file.i

编译器

  • 对预处理文件进行词法、语法和语义分析
        ○ 词法分析: 分析关键字,标识符, 立即数等是否合法
        ○ 语法分析: 分析表达式是否遵循语法规则
        ○ 语义分析: 在语法分析的基础上进一步分析表达式是否合法
  • 分析结束后进行代码优化生成相应的汇编文件
        ■ 编译指令示例:gcc -S file.i -0 file.s

汇编器

  • 汇编器将汇编代码转变为机器的可执行指令
  • 每条汇编语句几乎对应一条机器指令
        ■ 汇编指令示例:gcc -c file.s -o file.o

编程实验:源代码单步编译示例

test.c:

#include "test.h"

// Begin to define macro

#define GREETING "Hello world!"

#define INC(x) x++

// End

int main()
{   
    p = GREETING;
    
    INC(i);
    
    return 0;
}

test.h:

/*

    This is a header file.
    
*/


char* p = "Delphi";

int i = 0;
  • 预处理: gcc -E test.h test.c -o test.i

    test.i
# 1 "<command-line>"
# 1 "test.c"
# 1 "test.h" 1







char* p = "Delphi";

int i = 0;
# 2 "test.c" 2
# 11 "test.c"
int main()
{
    p = "Hello world!";

    i++;

    return 0;
}

  • 编译: gcc -S test.i -o test.s

    test.s
	.file	"test.c"
.globl p
	.section	.rodata
.LC0:
	.string	"Delphi"
	.data
	.align 4
	.type	p, @object
	.size	p, 4
p:
	.long	.LC0
.globl i
	.bss
	.align 4
	.type	i, @object
	.size	i, 4
i:
	.zero	4
	.section	.rodata
.LC1:
	.string	"Hello world!"
	.text
.globl main
	.type	main, @function
main:
	pushl	%ebp
	movl	%esp, %ebp
	movl	$.LC1, p
	movl	i, %eax
	addl	$1, %eax
	movl	%eax, i
	movl	$0, %eax
	popl	%ebp
	ret
	.size	main, .-main
	.ident	"GCC: (Ubuntu 4.4.1-4ubuntu8) 4.4.1"
	.section	.note.GNU-stack,"",@progbits
  • 汇编: gcc -c test.s -o test.o

    test.o 生成

在这里插入图片描述

关于详细中间步骤和gcc使用请见: gcc程序的编译过程和链接原理

小结

  • 编译过程分为预处理,编译,汇编和链接四个过程
        ○ 预处理: 处理注释, 宏以及已经以 # 开头的符号
        ○ 编译:进行词法分析,语法分析和语义分析
        ○ 汇编:将汇编代码翻译为机器指令的目标文件


内容参考狄泰软件学院系列课程,如有侵权,请联系作者删除!感谢~

发布了334 篇原创文章 · 获赞 661 · 访问量 130万+

猜你喜欢

转载自blog.csdn.net/czg13548930186/article/details/86711122