zoukankan      html  css  js  c++  java
  • C 编译过程浅析

     

        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参考

    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!
    ");
    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/

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    python 函数(二)
    python 函数(一)
    python 文件操作
    python标准数据类型 Bytes
    购物车代码
    关于python数据类型的一些举例
    栈的应用实例
    python 数据类型
    python相关的编码,运算
    Oracle篇 之 查询行及概念
  • 原文地址:https://www.cnblogs.com/standby/p/6784602.html
Copyright © 2011-2022 走看看