ARTICLE · 1416860
C语言入门(1):从源文件到二进制文件-一切始于源代码
C语言入门(1):从源文件到二进制文件-一切始于源代码本篇主要内容如下: 因此,写代码的本质,是用人类可读的方式,指挥机器完成精确的工作。理解从源代码到可执行文件的全过程,不仅能写出更高效的程序,更能深入掌握系统底层的运行机制——这也是 C 语言开发者的核心能力。 编译过程 这个过程可分为四个清晰阶段:预处理 → 编译 → 汇编 → 链接。 📄 源文件:
📝 输出:
📝 输出:
🔍 分析
输出: 关键输出: 🔍 分析
关键输出(地址已确定):
一切始于源代码 编译过程:预处理 → 编译 → 汇编 → 链接 小结
在计算机的世界里,一切始于源代码。
CPU 本身只能理解由 0 和 1 组成的机器指令——它无法直接执行我们写下的 if、for 或 printf 这样的文本。而人类也无法高效地用二进制去描述复杂的逻辑。于是,编程语言(如 C)成为人与机器之间的桥梁。
我们编写源代码,不是为了给 CPU 看,而是为了清晰、精确地表达我们的意图:如何处理数据、如何响应输入、如何控制硬件。随后,通过预处理器、编译器、汇编器和链接器这一系列工具链,源代码被逐步翻译成 CPU 能真正执行的二进制指令。
hello.c│▼ gcc -Ehello.i ← 预处理(宏、头文件展开)│▼ gcc -Shello.s ← 编译(C → 汇编)│▼ gcc -chello.o ← 汇编(汇编 → 机器码 + 符号)│▼ gcc (隐式调用 ld)a.out ← 链接(合并 .o + 库 → 可执行文件)
📄 源文件:hello.c
// hello.cintadd(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 %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0.seh_endprologuemovl %ecx, 16(%rbp)movl %edx, 24(%rbp)movl 16(%rbp), %edxmovl 24(%rbp), %eaxaddl %edx, %eaxpopq %rbpret.seh_endproc.globl main.def main; .scl 2; .type 32; .endef.seh_proc mainmain:pushq %rbp.seh_pushreg %rbpmovq %rsp, %rbp.seh_setframe %rbp, 0subq $48, %rsp # 为局部变量 c 分配栈空间.seh_stackalloc 48.seh_endprologuecall __mainmovl $2, %edx # 参数 b = 2movl $1, %ecx # 参数 a = 1call add # 调用 add(1, 2)movl %eax, -4(%rbp) # c = 返回值movl -4(%rbp), %eax # return caddq $48, %rsppopq %rbpret.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 %rbp1: 48 89 e5 mov %rsp,%rbp4: 89 4d 10 mov %ecx,0x10(%rbp)7: 89 55 18 mov %edx,0x18(%rbp)a: 8b 55 10 mov 0x10(%rbp),%edxd: 8b 45 18 mov 0x18(%rbp),%eax10: 01 d0 add %edx,%eax12: 5d pop %rbp13: c3 ret0000000000000014 <main>:14: 55 push %rbp15: 48 89 e5 mov %rsp,%rbp18: 48 83 ec 30 sub $0x30,%rsp1c: e8 00 00 00 00 call 21 <main+0xd># ← 地址为 0!需重定位21: ba 02 00 00 00 mov $0x2,%edx26: b9 01 00 00 00 mov $0x1,%ecx2b: e8 d0 ff ff ff call 0 <add>30: 89 45 fc mov %eax,-0x4(%rbp)33: 8b 45 fc mov -0x4(%rbp),%eax36: 48 83 c4 30 add $0x30,%rsp3a: 5d pop %rbp3b: c3 ret3c: 90 nop3d: 90 nop3e: 90 nop3f: 90 nop
# 查看符号表objdump -t hello.ohello.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 ret140001001: 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 %r15140001012: 41 56 push %r14140001014: 41 55 push %r13140001016: 41 54 push %r12140001018: 55 push %rbp140001019: 57 push %rdi14000101a: 56 push %rsi14000101b: 53 push %rbx14000101c: 48 83 ec 58 sub $0x58,%rsp140001020: b8 30 00 00 00 mov $0x30,%eax140001025: 65 67 48 8b 00 mov %gs:(%eax),%rax14000102a: 48 8b 70 08 mov 0x8(%rax),%rsi14000102e: 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 %rbp140001465: 48 89 e5 mov %rsp,%rbp140001468: 48 83 ec 30 sub $0x30,%rsp14000146c: e8 ef 00 00 00 call 140001560 <__main>140001471: ba 02 00 00 00 mov $0x2,%edx140001476: b9 01 00 00 00 mov $0x1,%ecx14000147b: e8 d0 ff ff ff call 140001450 <add>140001480: 89 45 fc mov %eax,-0x4(%rbp)140001483: 8b 45 fc mov -0x4(%rbp),%eax140001486: 48 83 c4 30 add $0x30,%rsp14000148a: 5d pop %rbp14000148b: c3 ret14000148c: 90 nop14000148d: 90 nop14000148e: 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 %rbp140001451: 48 89 e5 mov %rsp,%rbp140001454: 89 4d 10 mov %ecx,0x10(%rbp)140001457: 89 55 18 mov %edx,0x18(%rbp)14000145a: 8b 55 10 mov 0x10(%rbp),%edx14000145d: 8b 45 18 mov 0x18(%rbp),%eax140001460: 01 d0 add %edx,%eax140001462: 5d pop %rbp140001463: c3 ret
💡 重点分析
call 指令的操作数从00 00 00 00变为ef 00 00 00(小端序)。链接器还添加了启动代码( _start),负责设置环境并调用main。即使没用标准库,链接器仍会链接 crt1.o、crti.o等启动文件(可通过gcc -nostartfiles禁用,但需自行处理_start)。
🎯 小结
hello.c | hello.i | ||
hello.i | hello.s | ||
hello.s | hello.o | ||
hello.o | hello |















































































