zoukankan      html  css  js  c++  java
  • C语言学习笔记之 编译器编译流程

    编译器编译流程

    1. 预处理(.i)
    2. 编译(.s)
    3. 汇编(.o)
    4. 链接(.exe .out)

    示例文件:main.c

    // main.c
    #include <stdio.h>
    #define NAME 'C'
    
    // 主函数
    int main() {
        printf("Hello %c
    ",NAME);
        return 0;
    }
    

    预处理

    展开头文件,宏替换,去掉注释,条件编译

    命令:

    gcc -E main.c -o main.i
    

    main.i最后五行:

    # 5 "main.c"
    int main() {
        printf("Hello %c
    ",'C');
        return 0;
    }
    

    可以看到,注释已经被去掉,宏也被替换了

    编译

    检查语法,生成汇编

    命令:

    gcc -S main.i -o main.s
    

    main.s内容(汇编代码):

    	.file	"main.c"
    	.def	___main;	.scl	2;	.type	32;	.endef
    	.section .rdata,"dr"
    LC0:
    	.ascii "Hello %c12"
    	.text
    	.globl	_main
    	.def	_main;	.scl	2;	.type	32;	.endef
    _main:
    LFB13:
    	.cfi_startproc
    	pushl	%ebp
    	.cfi_def_cfa_offset 8
    	.cfi_offset 5, -8
    	movl	%esp, %ebp
    	.cfi_def_cfa_register 5
    	andl	$-16, %esp
    	subl	$16, %esp
    	call	___main
    	movl	$67, 4(%esp)
    	movl	$LC0, (%esp)
    	call	_printf
    	movl	$0, %eax
    	leave
    	.cfi_restore 5
    	.cfi_def_cfa 4, 4
    	ret
    	.cfi_endproc
    LFE13:
    	.ident	"GCC: (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 5.3.0"
    	.def	_printf;	.scl	2;	.type	32;	.endef
    

    汇编

    汇编代码转机器码

    命令:

    gcc -c main.s -o main.o
    

    此时main.o是二进制文件,不能直接打开
    Python中打开内容如下:

    b'Lx01x06x00x00x00x00x00xdcx01x00x00x12x00x00x00x00x00x04x01.textx00x00x00x00x00x00x00x00x00x00x00,x00x00x00x04x01x00x00xb4x01x00x00x00x00x00x00x03x00x00x00 x000`.datax00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00@x000xc0.bssx00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x80x000xc0.rdatax00x00x00x00x00x00x00x00x00x00x0cx00x00x000x01x00x00x00x00x00x00x00x00x00x00x00x00x00x00@x000@/4x00x00x00x00x00x00x00x00x00x00x00x00x00x00@x00x00x00<x01x00x00x00x00x00x00x00x00x00x00x00x00x00x00@x000@/15x00x00x00x00x00x00x00x00x00x00x00x00x008x00x00x00|x01x00x00xd2x01x00x00x00x00x00x00x01x00x00x00@x000@Ux89xe5x83xe4xf0x83xecx10xe8x00x00x00x00xc7D$x04Cx00x00x00xc7x04$x00x00x00x00xe8x00x00x00x00xb8x00x00x00x00xc9xc3x90x90x90Hello %c
    x00x00x00GCC: (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 5.3.0x00x00x14x00x00x00x00x00x00x00x01zRx00x01|x08x01x1bx0cx04x04x88x01x00x00x1cx00x00x00x1cx00x00x00x04x00x00x00)x00x00x00x00Ax0ex08x85x02B
    x05exc5x0cx04x04x00x00
    x00x00x00x10x00x00x00x14x00x19x00x00x00
    x00x00x00x06x00x1ex00x00x00x11x00x00x00x14x00 x00x00x00x04x00x00x00x14x00.filex00x00x00x00x00x00x00xfexffx00x00gx01main.cx00x00x00x00x00x00x00x00x00x00x00x00_mainx00x00x00x00x00x00x00x01x00 x00x02x01x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00.textx00x00x00x00x00x00x00x01x00x00x00x03x01)x00x00x00x03x00x00x00x00x00x00x00x00x00x00x00x00x00.datax00x00x00x00x00x00x00x02x00x00x00x03x01x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00.bssx00x00x00x00x00x00x00x00x03x00x00x00x03x01x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00.rdatax00x00x00x00x00x00x04x00x00x00x03x01
    x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x19x00x00x00x00x00x00x00x05x00x00x00x03x01?x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00$x00x00x00x00x00x00x00x06x00x00x00x03x018x00x00x00x01x00x00x00x00x00x00x00x00x00x00x00x00x00___mainx00x00x00x00x00x00x00 x00x02x00_printfx00x00x00x00x00x00x00 x00x02x00.x00x00x00.rdata$zzzx00.eh_framex00.rdata$zzzx00.eh_framex00'
    

    链接

    链接到一起生成可执行文件

    命令:

    gcc main.o -o main
    
  • 相关阅读:
    C语言 · 新生舞会
    C语言 · 集合运算
    C语言 · 字符串的展开
    C语言 · 学做菜
    C语言 · 最长公共子序列 · 最长字符序列
    C语言 · 复数求和
    C语言 · 扶老奶奶过街
    C语言 · 删除重复元素
    1-3
    1-4
  • 原文地址:https://www.cnblogs.com/zhujiangyu/p/13586356.html
Copyright © 2011-2022 走看看