第一步 编辑源代码
filename:test.c
#include<stdio.h>
int main()
{
printf("hello world!");
return 0;
}
第二步 预处理阶段
编译器将上述代码中的“stdio.h"编译进来,在此可以用gcc的参数"-E"指定gcc只在预处理结束后才停止编译过程
gcc test.c -o test.i -E
生成test.i文件
第三步 编译阶段
检查语法错误,然后将代码翻译成汇编语言
gcc test.i -o test.s -S
生成了test.s文件
test.s文件内容
.file "test.c"
.text
.section .rodata
.LC0:
.string "hello world!"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
endbr64
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
leaq .LC0(%rip), %rdi
movl $0, %eax
call printf@PLT
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 9.3.0-10ubuntu2) 9.3.0"
.section .note.GNU-stack,"",@progbits
.section .note.gnu.property,"a"
.align 8
.long 1f - 0f
.long 4f - 1f
.long 5
0:
.string "GNU"
1:
.align 8
1,1-8 顶端
第四步 汇编阶段
就是将编译阶段生成的".s"文件转成目标文件。
gcc test.s -o test.o -c
第四步 链接阶段
把程序中一些函数实现部分链接起来
gcc test.o -o test
运行阶段
./test