C 编译过程浅析

时间:2023-03-09 03:44:09
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.



 [root@standby gcc]# date +%F_%H:%M:%S
--29_23::
[root@standby gcc]# pwd
/data/gcc
[root@standby gcc]# ll
total
-rw-r--r-- root root Nov : hello.c
[root@standby gcc]# cat hello.c
#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
return ;
}
[root@standby gcc]#
[root@standby gcc]# file hello.c
hello.c: ASCII C program text
[root@standby gcc]#

预处理/Preprocessing

'删除注释和";"、进行宏替换、将头文件插入进来、条件编译等'
 [root@standby gcc]# gcc -E hello.c -o hello.i
[root@standby gcc]# ll
total
-rw-r--r-- root root Nov : hello.c
-rw-r--r-- root root Nov : hello.i
[root@standby gcc]# file hello.i
hello.i: ASCII C program text
[root@standby gcc]#

编译/Compilation

'生成汇编代码'
 [root@standby gcc]# gcc -S hello.i -o hello.s
[root@standby gcc]# ll
total
-rw-r--r-- root root Nov : hello.c
-rw-r--r-- root root Nov : hello.i
-rw-r--r-- root root Nov : hello.s
[root@standby gcc]# file hello.s
hello.s: ASCII assembler program text
[root@standby gcc]#

汇编/Assembly

'生成目标文件'
 [root@standby gcc]# gcc -c hello.s -o hello.o
[root@standby gcc]# ll
total
-rw-r--r-- root root Nov : hello.c
-rw-r--r-- root root Nov : hello.i
-rw-r--r-- root root Nov : hello.o
-rw-r--r-- root root Nov : hello.s
[root@standby gcc]# file hello.o
hello.o: ELF -bit LSB relocatable, x86-, version (SYSV), not stripped
[root@standby gcc]#

连接/Linking

'生成可执行文件'
 [root@standby gcc]# gcc hello.o -o helloworld
[root@standby gcc]# ll
total
-rw-r--r-- root root Nov : hello.c
-rw-r--r-- root root Nov : hello.i
-rw-r--r-- root root Nov : hello.o
-rw-r--r-- root root Nov : hello.s
-rwxr-xr-x root root Nov : helloworld
[root@standby gcc]# file helloworld
helloworld: ELF -bit LSB executable, x86-, version (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6., not stripped
[root@standby gcc]#
[root@standby gcc]# ./helloworld
Hello World!
[root@standby gcc]#



汇编代码

 [root@standby gcc]# cat hello.s
.file "hello.c"
.section .rodata
.LC0:
.string "Hello World!"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset
.cfi_offset , -
movq %rsp, %rbp
.cfi_def_cfa_register
movl $.LC0, %edi
call puts
movl $, %eax
leave
.cfi_def_cfa ,
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-17)"
.section .note.GNU-stack,"",@progbits
[root@standby gcc]#