企业免费建站网站,一加开发者模式怎么关闭,商业街网站建设方案,做招聘网站要多久从C源代码到可执行文件的四个过程#xff1a;预处理、编译、汇编、链接
总览 我们将在Linux操作系统中#xff0c;以C语言的Hello World程序为例#xff0c;用gcc编译器分步执行这四个步骤。
我们有再熟悉不过的HelloWorld程序#xff0c;hello.c#xff1a;
#include …从C源代码到可执行文件的四个过程预处理、编译、汇编、链接
总览 我们将在Linux操作系统中以C语言的Hello World程序为例用gcc编译器分步执行这四个步骤。
我们有再熟悉不过的HelloWorld程序hello.c
#include stdio.hint main(){printf(Hellow World.\n);return 0;
}预处理 预处理阶段 预处理器cpp根据以#字节开头的命令修改原始的C程序 执行预处理命令gcc -E hello.c -o hello.i我们得到由.c文件得到.i文件hello.i
# 1 hello.c
# 1 built-in
# 1 command-line
# 31 command-line
# 1 /usr/include/stdc-predef.h 1 3 4
# 32 command-line 2
# 1 hello.c
# 1 /usr/include/stdio.h 1 3 4
# 27 /usr/include/stdio.h 3 4
# 1 /usr/include/x86_64-linux-gnu/bits/libc-header-start.h 1 3 4
# 33 /usr/include/x86_64-linux-gnu/bits/libc-header-start.h 3 4
# 1 /usr/include/features.h 1 3 4
# 424 /usr/include/features.h 3 4
# 1 /usr/include/x86_64-linux-gnu/sys/cdefs.h 1 3 4
# 427 /usr/include/x86_64-linux-gnu/sys/cdefs.h 3 4
# 1 /usr/include/x86_64-linux-gnu/bits/wordsize.h 1 3 4
# 428 /usr/include/x86_64-linux-gnu/sys/cdefs.h 2 3 4
# 1 /usr/include/x86_64-linux-gnu/bits/long-double.h 1 3 4
# 429 /usr/include/x86_64-linux-gnu/sys/cdefs.h 2 3 4
# 425 /usr/include/features.h 2 3 4
# 448 /usr/include/features.h 3 4
# 1 /usr/include/x86_64-linux-gnu/gnu/stubs.h 1 3 4
# 10 /usr/include/x86_64-linux-gnu/gnu/stubs.h 3 4
# 1 /usr/include/x86_64-linux-gnu/gnu/stubs-64.h 1 3 4
# 11 /usr/include/x86_64-linux-gnu/gnu/stubs.h 2 3 4
# 449 /usr/include/features.h 2 3 4
# 34 /usr/include/x86_64-linux-gnu/bits/libc-header-start.h 2 3 4
# 28 /usr/include/stdio.h 2 3 4编译 编译阶段 编译器cc1将文本文件hello.i翻译成文本文件hello.s它包含一个汇编语言程序。 执行编译命令gcc -S hello.i -o hello.s我们得到由.i文件得到.s文件hello.s这就是汇编文件 .file hello.c.text.section .rodata
.LC0:.string Hellow World..text.globl main.type main, function
main:
.LFB0:.cfi_startprocpushq %rbp.cfi_def_cfa_offset 16.cfi_offset 6, -16movq %rsp, %rbp.cfi_def_cfa_register 6leaq .LC0(%rip), %rdicall putsPLTmovl $0, %eaxpopq %rbp.cfi_def_cfa 7, 8ret.cfi_endproc
.LFE0:.size main, .-main.ident GCC: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0.section .note.GNU-stack,,progbits
汇编 汇编阶段 汇编器as将hello.s翻译成机器语言指令把这些指令打包成一种叫做可重定位目标程序的格式并保存在hello.o文件中这是一个二进制文件无法直接用文本编辑器查看。 执行编译命令gcc -c hello.s -o hello.o我们得到由.s文件得到.o文件hello.o可重定向文件文件这个文件就不是文本文件了因此无法展示。
至此以上三步其实可以由-c参数直接得到可重定向文件gcc -c hello.c -o hello.o以上是为了说明预处理和编译两步专门分步进行以查看输出。
链接 链接阶段 hello程序调用了printf函数它来自C标准库具体存在于一个已经预编译好的printf.o文件链接器ld负责将这个文件与我们的hello文件合并起来。 执行链接命令gcc hello.o -o hello我们有.s文件得到可执行文件hello直接./hello执行即可在命令行打印输出Hellow World。
RefCSAPP