一 点睛
gcc对C/C++的编译过程分为4个阶段:预处理、编译、汇编和链接。
二 预处理
1 源代码test.c
#include <stdio.h>
int main(int argc,char *argv[])
{
printf("hello, boy \n" );
return 0;
}
2 预处理
[root@localhost test]# gcc -E test.c -o test.i
3 产生test.i文件内容
三 编译
1 编译命令
[root@localhost test]# gcc -S test.i -o test.s
2 编译产生test.s
.file "test.c"
.section .rodata
.LC0:
.string "hello, boy "
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl %edi, -4(%rbp)
movq %rsi, -16(%rbp)
movl $.LC0, %edi
call puts
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-16)"
.section .note.GNU-stack,"",@progbits
四 汇编
1 汇编语句
[root@localhost test]# gcc -c test.s -o test.o
2 产生目标文件test.o
[root@localhost test]# hexdump test.o
0000000 457f 464c 0102 0001 0000 0000 0000 0000
0000010 0001 003e 0001 0000 0000 0000 0000 0000
0000020 0000 0000 0000 0000 02a8 0000 0000 0000
0000030 0000 0000 0040 0000 0000 0040 000d 000a
0000040 4855 e589 8348 10ec 7d89 48fc 7589 bff0
......
0000570 0000 0000 0000 0000 0000 0000 0000 0000
0000580 0140 0000 0000 0000 0108 0000 0000 0000
0000590 000c 0000 0009 0000 0008 0000 0000 0000
00005a0 0018 0000 0000 0000 0009 0000 0003 0000
00005b0 0000 0000 0000 0000 0000 0000 0000 0000
00005c0 0248 0000 0000 0000 0012 0000 0000 0000
00005d0 0000 0000 0000 0000 0001 0000 0000 0000
00005e0 0000 0000 0000 0000
00005e8
五 链接
1 链接语句
[root@localhost test]# gcc test.o -o test
六 运行
[root@localhost test]# ./test
hello, boy
七 gcc支持的具有不同后缀名的文件
后缀名 | 文件名 | 后续编译流程 |
.c | C源代码文件 | 预处理、编译、汇编、链接 |
.C/.cc/.cxx/.cpp/c++ | C++源代码文件 | 预处理、编译、汇编、链接 |
.m | Object-C源代码文件 | 预处理、编译、汇编、链接 |
.i | 已经预处理过的C源代码文件 | 编译、汇编、链接 |
.ii | 已经预处理过的C++源代码文件 | 编译、汇编、链接 |
.s | 汇编语句源代码文件 | 汇编、链接 |
.S | 经过预编译的汇编语句源代码文件 | 汇编、链接 |
.a | 由目标文件构成的档案库文件(静态库) | 链接 |
.o | 编译后的目标文件(二进制文件) | 链接 |