C 编译过程浅析
1 2 3 4 5 | From where i stand, there are two programmig languages in the world, which is C lang and the other. |
编译(compile)
- 预处理(也称预编译,Preprocessing)
- 编译(Compilation)
- 汇编 (Assembly)
- 连接(Linking)
gcc - GNU project C and C++ compiler
If you only want some of the stages of compilation, you can use -x (or filename suffixes) to tell gcc where to start, and one of the options -c, -S, or -E to say where gcc is to stop. Note that some combinations (for example, -x cpp-output -E) instruct gcc to do nothing at all.
-E Stop after the preprocessing stage; do not run the compiler proper.
-S Stop after the stage of compilation proper; do not assemble.
-c Compile or assemble the source files, but do not link.
-o targetfile
Place output in file targetfile.
1 [root@standby gcc]# date +%F_%H:%M:%S 2 2016-11-29_23:31:29 3 [root@standby gcc]# pwd 4 /data/gcc 5 [root@standby gcc]# ll 6 total 4 7 -rw-r--r-- 1 root root 82 Nov 29 23:27 hello.c 8 [root@standby gcc]# cat hello.c 9 #include <stdio.h> 10 int main(void) 11 { 12 printf("Hello World!\n"); 13 return 0; 14 } 15 [root@standby gcc]# 16 [root@standby gcc]# file hello.c 17 hello.c: ASCII C program text 18 [root@standby gcc]#
预处理/Preprocessing
'删除注释和";"、进行宏替换、将头文件插入进来、条件编译等'
1 [root@standby gcc]# gcc -E hello.c -o hello.i 2 [root@standby gcc]# ll 3 total 24 4 -rw-r--r-- 1 root root 82 Nov 29 23:27 hello.c 5 -rw-r--r-- 1 root root 16742 Nov 29 23:58 hello.i 6 [root@standby gcc]# file hello.i 7 hello.i: ASCII C program text 8 [root@standby gcc]#
编译/Compilation
'生成汇编代码'
1 [root@standby gcc]# gcc -S hello.i -o hello.s 2 [root@standby gcc]# ll 3 total 28 4 -rw-r--r-- 1 root root 82 Nov 29 23:27 hello.c 5 -rw-r--r-- 1 root root 16742 Nov 29 23:58 hello.i 6 -rw-r--r-- 1 root root 444 Nov 30 00:00 hello.s 7 [root@standby gcc]# file hello.s 8 hello.s: ASCII assembler program text 9 [root@standby gcc]#
汇编/Assembly
'生成目标文件'
1 [root@standby gcc]# gcc -c hello.s -o hello.o 2 [root@standby gcc]# ll 3 total 32 4 -rw-r--r-- 1 root root 82 Nov 29 23:27 hello.c 5 -rw-r--r-- 1 root root 16742 Nov 29 23:58 hello.i 6 -rw-r--r-- 1 root root 1504 Nov 30 00:03 hello.o 7 -rw-r--r-- 1 root root 444 Nov 30 00:00 hello.s 8 [root@standby gcc]# file hello.o 9 hello.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped 10 [root@standby gcc]#
连接/Linking
'生成可执行文件'
1 [root@standby gcc]# gcc hello.o -o helloworld 2 [root@standby gcc]# ll 3 total 40 4 -rw-r--r-- 1 root root 82 Nov 29 23:27 hello.c 5 -rw-r--r-- 1 root root 16742 Nov 29 23:58 hello.i 6 -rw-r--r-- 1 root root 1504 Nov 30 00:03 hello.o 7 -rw-r--r-- 1 root root 444 Nov 30 00:00 hello.s 8 -rwxr-xr-x 1 root root 6425 Nov 30 00:04 helloworld 9 [root@standby gcc]# file helloworld 10 helloworld: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped 11 [root@standby gcc]# 12 [root@standby gcc]# ./helloworld 13 Hello World! 14 [root@standby gcc]#
汇编代码
1 [root@standby gcc]# cat hello.s 2 .file "hello.c" 3 .section .rodata 4 .LC0: 5 .string "Hello World!" 6 .text 7 .globl main 8 .type main, @function 9 main: 10 .LFB0: 11 .cfi_startproc 12 pushq %rbp 13 .cfi_def_cfa_offset 16 14 .cfi_offset 6, -16 15 movq %rsp, %rbp 16 .cfi_def_cfa_register 6 17 movl $.LC0, %edi 18 call puts 19 movl $0, %eax 20 leave 21 .cfi_def_cfa 7, 8 22 ret 23 .cfi_endproc 24 .LFE0: 25 .size main, .-main 26 .ident "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-17)" 27 .section .note.GNU-stack,"",@progbits 28 [root@standby gcc]#
作者:Standby — 一生热爱名山大川、草原沙漠,还有我们小郭宝贝!
出处:http://www.cnblogs.com/standby/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://www.cnblogs.com/standby/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步