码迷,mamicode.com
首页 > 其他好文 > 详细

C 编译过程浅析

时间:2017-04-29 10:52:59      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:offset   ++   project   x86   htm   void   std   插入   link   

  From where i stand, there are two programmig languages in the world, which is C lang and the other.

                                                                    --- standby

编译(compile)

  • 预处理(也称预编译,Preprocessing)
  • 编译(Compilation)
  • 汇编 (Assembly)
  • 连接(Linking)

GCC参考

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]#

 

C 编译过程浅析

标签:offset   ++   project   x86   htm   void   std   插入   link   

原文地址:http://www.cnblogs.com/standby/p/6784602.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!