夜雨聆风学习资料网

ARTICLE · 1416860

C语言入门(1):从源文件到二进制文件-一切始于源代码

C语言入门(1):从源文件到二进制文件-一切始于源代码
本篇主要内容如下:
  • 一切始于源代码
  • 编译过程:预处理 → 编译 → 汇编 → 链接
  • 小结

在计算机的世界里,一切始于源代码。

CPU 本身只能理解由 0 和 1 组成的机器指令——它无法直接执行我们写下的 if、for 或 printf 这样的文本。而人类也无法高效地用二进制去描述复杂的逻辑。于是,编程语言(如 C)成为人与机器之间的桥梁。

我们编写源代码,不是为了给 CPU 看,而是为了清晰、精确地表达我们的意图:如何处理数据、如何响应输入、如何控制硬件。随后,通过预处理器、编译器、汇编器和链接器这一系列工具链,源代码被逐步翻译成 CPU 能真正执行的二进制指令。

因此,写代码的本质,是用人类可读的方式,指挥机器完成精确的工作。理解从源代码到可执行文件的全过程,不仅能写出更高效的程序,更能深入掌握系统底层的运行机制——这也是 C 语言开发者的核心能力。
编译过程
这个过程可分为四个清晰阶段:预处理 → 编译 → 汇编 → 链接。
hello.c   │   ▼  gcc -Ehello.i   ← 预处理(宏、头文件展开)   │   ▼  gcc -Shello.s   ← 编译(C → 汇编)   │   ▼  gcc -chello.o   ← 汇编(汇编 → 机器码 + 符号)   │   ▼  gcc (隐式调用 ld)a.out     ← 链接(合并 .o + 库 → 可执行文件)

📄 源文件:hello.c

// hello.c intadd(int a, int b) {    return a + b;}intmain() {    int c = add(1, 2);    return c;}

第一阶段:预处理(Preprocessing)

✅ 目的

展开宏、包含头文件、处理条件编译指令,生成纯 C 代码。

🔧 命令

gcc -E hello.c -o hello.i

📝 输出:hello.i

由于 hello.c 中没有 #include、#define 或条件编译,预处理器几乎不做改动,仅添加少量内置信息(如行号标记):

# 0 "hello.c"# 0 "<built-in>"# 0 "<command-line>"# 1 "hello.c"intadd(int a, int b) {    return a + b;}intmain() {    int c = add(1, 2);    return c;}

💡 重点分析

  • 预处理器是“文本替换器”,不理解 C 语法。  
  • 若代码中有 #include <stdio.h>,此处会插入数千行头文件内容。  
  • 使用 gcc -dD -E 可查看所有宏定义,如下:
# gcc -dD -E hello.c# 0 "hello.c"# 0 "<built-in>"#define __STDC__ 1# 0 "<built-in>"#define __STDC_VERSION__ 202311L# 0 "<built-in>"#define __STDC_UTF_16__ 1# 0 "<built-in>"#define __STDC_UTF_32__ 1# 0 "<built-in>"#define __STDC_HOSTED__ 1# 0 "<built-in>"#define __STDC_EMBED_NOT_FOUND__ 0...<此处略># 0 "<built-in>"#define _WIN64 1# 0 "<built-in>"#define __USING_POSIXTHREAD__ 1# 0 "<built-in>"#define __declspec(x) __attribute__((x))# 0 "<built-in>"#define __DECIMAL_BID_FORMAT__ 1# 0 "<command-line>"#define _REENTRANT 1# 1 "hello.c"intadd(int a, int b) {    return a + b;}intmain() {    int c = add(1, 2);    return c;}

第二阶段:编译(Compilation → 生成汇编)

✅ 目的

将预处理后的 C 代码翻译成目标平台的汇编语言(如 x86-64)。

🔧 命令

gcc -S -O0 hello.c -o hello.s   # -O0 禁用优化,便于理解

📝 输出:hello.s(x86-64 AT&T 语法)

	.file	"hello.c"	.text	.globl	add	.def	add;	.scl	2;	.type	32;	.endef	.seh_proc	addadd:	pushq	%rbp	.seh_pushreg	%rbp	movq	%rsp, %rbp	.seh_setframe	%rbp, 0	.seh_endprologue	movl	%ecx, 16(%rbp)	movl	%edx, 24(%rbp)	movl	16(%rbp), %edx	movl	24(%rbp), %eax	addl	%edx, %eax	popq	%rbp	ret	.seh_endproc	.globl	main	.def	main;	.scl	2;	.type	32;	.endef	.seh_proc	mainmain:	pushq	%rbp	.seh_pushreg	%rbp	movq	%rsp, %rbp	.seh_setframe	%rbp, 0	subq	$48, %rsp # 为局部变量 c 分配栈空间	.seh_stackalloc	48	.seh_endprologue	call	__main	movl	$2, %edx # 参数 b = 2	movl	$1, %ecx # 参数 a = 1	call	add      # 调用 add(1, 2)	movl	%eax, -4(%rbp) # c = 返回值	movl	-4(%rbp), %eax # return c	addq	$48, %rsp	popq	%rbp	ret	.seh_endproc	.def	__main;	.scl	2;	.type	32;	.endef	.ident	"GCC: (Rev8, Built by MSYS2 project) 15.2.0"

💡 重点分析

  • 函数参数通过寄存器传递(x86-64 ABI:%edx, %ecx)。  
  • 局部变量 c 存放在栈上([rbp-4])。  
  • call add 是符号引用,实际地址由链接器填充。  
  • 若启用 -O2,整个 main 可能被优化为 mov $3, %eax; ret!

第三阶段:汇编(Assembly → 生成目标文件)

✅ 目的

将汇编代码转换为机器码,生成包含代码、数据、符号表的目标文件(.o)。

🔧 命令

gcc -c hello.s -o hello.o# 或直接:gcc -c hello.c -o hello.o

🔍 分析 hello.o(使用 objdump 和 readelf)

# 查看反汇编(验证机器码)objdump -d hello.o
输出:
hello.o:     file format pe-x86-64Disassembly of section .text:0000000000000000 <add>:   0:   55                      push   %rbp   1:   48 89 e5                mov    %rsp,%rbp   4:   89 4d 10                mov    %ecx,0x10(%rbp)   7:   89 55 18                mov    %edx,0x18(%rbp)   a:   8b 55 10                mov    0x10(%rbp),%edx   d:   8b 45 18                mov    0x18(%rbp),%eax  10:   01 d0                   add    %edx,%eax  12:   5d                      pop    %rbp  13:   c3                      ret0000000000000014 <main>:  14:   55                      push   %rbp  15:   48 89 e5                mov    %rsp,%rbp  18:   48 83 ec 30             sub    $0x30,%rsp  1c:   e8 00 00 00 00          call   21 <main+0xd># ← 地址为 0!需重定位  21:   ba 02 00 00 00          mov    $0x2,%edx  26:   b9 01 00 00 00          mov    $0x1,%ecx  2b:   e8 d0 ff ff ff          call   0 <add>  30:   89 45 fc                mov    %eax,-0x4(%rbp)  33:   8b 45 fc                mov    -0x4(%rbp),%eax  36:   48 83 c4 30             add    $0x30,%rsp  3a:   5d                      pop    %rbp  3b:   c3                      ret  3c:   90                      nop  3d:   90                      nop  3e:   90                      nop  3f:   90                      nop
# 查看符号表objdump -t hello.o
关键输出:
hello.o:     file format pe-x86-64SYMBOL TABLE:[  0](sec -2)(fl 0x00)(ty    0)(scl 103) (nx 1) 0x0000000000000000 hello.cFile[  2](sec  1)(fl 0x00)(ty   20)(scl   2) (nx 1) 0x0000000000000000 addAUX tagndx 0 ttlsiz 0x0 lnnos 0 next 4[  4](sec  1)(fl 0x00)(ty   20)(scl   2) (nx 1) 0x0000000000000014 mainAUX tagndx 0 ttlsiz 0x0 lnnos 0 next 0[  6](sec  1)(fl 0x00)(ty    0)(scl   3) (nx 1) 0x0000000000000000 .textAUX scnlen 0x3c nreloc 1 nlnno 0[  8](sec  2)(fl 0x00)(ty    0)(scl   3) (nx 1) 0x0000000000000000 .dataAUX scnlen 0x0 nreloc 0 nlnno 0[ 10](sec  3)(fl 0x00)(ty    0)(scl   3) (nx 1) 0x0000000000000000 .bssAUX scnlen 0x0 nreloc 0 nlnno 0[ 12](sec  4)(fl 0x00)(ty    0)(scl   3) (nx 1) 0x0000000000000000 .xdataAUX scnlen 0x14 nreloc 0 nlnno 0[ 14](sec  5)(fl 0x00)(ty    0)(scl   3) (nx 1) 0x0000000000000000 .pdataAUX scnlen 0x18 nreloc 6 nlnno 0[ 16](sec  6)(fl 0x00)(ty    0)(scl   3) (nx 1) 0x0000000000000000 .rdata$zzzAUX scnlen 0x2b nreloc 0 nlnno 0[ 18](sec  0)(fl 0x00)(ty   20)(scl   2) (nx 0) 0x0000000000000000 __main

💡 重点分析:  

  • call 21 的机器码是 e8 00 00 00 00 —— 调用地址为 0!
  • 这是因为 add 的地址尚未确定,链接器需修正(重定位)。  
  • 符号表中标记 add 和 main 为 FUNC(函数),且 nx=1 表示在 .text 段。

第四阶段:链接(Linking → 生成可执行文件)

✅ 目的

合并目标文件,解析符号引用,分配最终地址,生成可执行文件。

🔧 命令

gcc hello.o -o hello# 或直接:gcc hello.c -o hello

🔍 分析 hello(可执行文件)

# 查看最终反汇编objdump -d hello.exe
关键输出(地址已确定):
hello.exe:     file format pei-x86-64Disassembly of section .text:0000000140001000 <__mingw_invalidParameterHandler>:   140001000:   c3                      ret   140001001:   0f 1f 40 00             nopl   0x0(%rax)   140001005:   66 66 2e 0f 1f 84 00    data16 cs nopw 0x0(%rax,%rax,1)   14000100c:   00 00 00 000000000140001010 <__tmainCRTStartup>:   140001010:   41 57                   push   %r15   140001012:   41 56                   push   %r14   140001014:   41 55                   push   %r13   140001016:   41 54                   push   %r12   140001018:   55                      push   %rbp   140001019:   57                      push   %rdi   14000101a:   56                      push   %rsi   14000101b:   53                      push   %rbx   14000101c:   48 83 ec 58             sub    $0x58,%rsp   140001020:   b8 30 00 00 00          mov    $0x30,%eax   140001025:   65 67 48 8b 00          mov    %gs:(%eax),%rax   14000102a:   48 8b 70 08             mov    0x8(%rax),%rsi   14000102e:   48 8b 1d fb 33 00 00    mov    0x33fb(%rip),%rbx        # 140004430 <.refptr.__native_startup_lock>   140001035:   48 8b 3d 4c 71 00 00    mov    0x714c(%rip),%rdi        # 140008188 <__imp_Sleep>   14000103c:   eb 12                   jmp    140001050 <__tmainCRTStartup+0x40>...<此处略>0000000140001464 <main>:   140001464:   55                      push   %rbp   140001465:   48 89 e5                mov    %rsp,%rbp   140001468:   48 83 ec 30             sub    $0x30,%rsp   14000146c:   e8 ef 00 00 00          call   140001560 <__main>   140001471:   ba 02 00 00 00          mov    $0x2,%edx   140001476:   b9 01 00 00 00          mov    $0x1,%ecx   14000147b:   e8 d0 ff ff ff          call   140001450 <add>   140001480:   89 45 fc                mov    %eax,-0x4(%rbp)   140001483:   8b 45 fc                mov    -0x4(%rbp),%eax   140001486:   48 83 c4 30             add    $0x30,%rsp   14000148a:   5d                      pop    %rbp   14000148b:   c3                      ret   14000148c:   90                      nop   14000148d:   90                      nop   14000148e:   90                      nop
0000000140001404 <.l_start>:   140001404:   48 8b 05 d5 2f 00 00    mov    0x2fd5(%rip),%rax        # 1400043e0 <.refptr.__mingw_app_type>   14000140b:   c7 00 00 00 00 00       movl   $0x0,(%rax)   140001411:   e8 fa fb ff ff          call   140001010 <__tmainCRTStartup>   140001416:   90  
0000000140001450 <add>:   140001450:   55                      push   %rbp   140001451:   48 89 e5                mov    %rsp,%rbp   140001454:   89 4d 10                mov    %ecx,0x10(%rbp)   140001457:   89 55 18                mov    %edx,0x18(%rbp)   14000145a:   8b 55 10                mov    0x10(%rbp),%edx   14000145d:   8b 45 18                mov    0x18(%rbp),%eax   140001460:   01 d0                   add    %edx,%eax   140001462:   5d                      pop    %rbp   140001463:   c3                      ret

💡 重点分析  

  • call 指令的操作数从 00 00 00 00 变为 ef 00 00 00(小端序)。
  • 链接器还添加了启动代码(_start),负责设置环境并调用 main。  
  • 即使没用标准库,链接器仍会链接 crt1.o、crti.o 等启动文件(可通过 gcc -nostartfiles 禁用,但需自行处理 _start)。

🎯 小结

阶段
输入
输出
关键动作
预处理
hello.chello.i
宏展开、头文件包含
编译
hello.ihello.s
C → 汇编(语法/语义分析)
汇编
hello.shello.o
汇编 → 机器码 + 符号表
链接
hello.ohello
符号解析 + 地址分配 + 重定位

回首页,搜索你想要的文档

在首页搜索框输入关键词,快速定位资料

文库首页搜索指引 立即搜索

相关学习资料