# TinyCC

**TinyCC**（也写作 TinyCC，其可执行文件名为 **tcc**），全称 Tiny C 编译器，是一款小巧、快速的 C 编译器，最初由[法布里斯·贝拉尔](https://www.edgepedia.org/zh/fabrice-bellard)（Fabrice Bellard）编写。它单遍直接生成机器码，无需外部工具即可编译、汇编和链接 C 程序。它面向 i386、x86_64、ARM、AArch64 和 riscv64 平台，其 README 声称编译和链接速度约为 `gcc -O0` 的 10 倍<sup>[1](https://github.com/TinyCC/tinycc/blob/mob/README)</sup>。该编译器在 [GNU 宽通用公共许可证](https://www.edgepedia.org/zh/gnu-lesser-general-public-license)下分发，并且能够编译自身的源代码<sup>[1](https://github.com/TinyCC/tinycc/blob/mob/README)</sup>。

| 关键事实 | 详情 |
|---|---|
| 作者与许可证 | 由法布里斯·贝拉尔编写；帮助文本带有版权声明 (C) 2001-2006；在 GNU LGPL 下分发<sup>[2](https://github.com/TinyCC/tinycc/blob/mob/tcc.c)</sup><sup> • </sup><sup>[1](https://github.com/TinyCC/tinycc/blob/mob/README)</sup> |
| 目标平台 | 面向 i386、x86_64、arm、aarch64 或 riscv64 的机器码<sup>[1](https://github.com/TinyCC/tinycc/blob/mob/README)</sup> |
| 编译速度 | 编译和链接速度约为 `gcc -O0` 的 10 倍（该声称的基线是未优化的 GCC）<sup>[1](https://github.com/TinyCC/tinycc/blob/mob/README)</sup> |
| 体积 | x86 平台的 tcc 可执行文件约需 100 kB<sup>[3](https://www.complang.tuwien.ac.at/Diplomarbeiten/falbesoner14.pdf)</sup> |
| 语言标准 | 实现 ANSI C（C89/C90）及大部分 C99；README 称 TCC 正“迈向完整的 ISOC99 合规”<sup>[3](https://www.complang.tuwien.ac.at/Diplomarbeiten/falbesoner14.pdf)</sup><sup> • </sup><sup>[1](https://github.com/TinyCC/tinycc/blob/mob/README)</sup> |
| 脚本化 | `tcc -run file.c` 将代码编译到内存并立即执行；首行为 `#!/usr/local/bin/tcc -run` 的 C 文件可像 shell 脚本一样运行<sup>[2](https://github.com/TinyCC/tinycc/blob/mob/tcc.c)</sup><sup> • </sup><sup>[1](https://github.com/TinyCC/tinycc/blob/mob/README)</sup> |
| 最新标记版本 | 0.9.27，标记于 2017-12-12；开发在 `mob` 分支上持续进行<sup>[4](https://www.repo.or.cz/tinycc.git)</sup> |

## 源自一个混淆代码编译器

TinyCC 脱胎于一件参赛作品。贝拉尔为赢得 2002 年国际混淆 C 代码竞赛（International Obfuscated C Code Contest）而编写了混淆 Tiny C 编译器（Obfuscated Tiny C Compiler，OTCC），目标是写出能够编译自身的最小 C 编译器。竞赛规则将源码限制在 2048 字节的 C 代码以内（不计 `;`、`{`、`}` 和空格字符），因此 OTCC 仅支持 C 的一个子集，生成 i386 代码，并用 `dlsym()` 解析外部符号。一个可移植的变体 OTCCELF 无需 binutils 即可直接生成动态链接的 i386 ELF 可执行文件。贝拉尔表示，功能完备的 ISOC99 C 编译器 TinyCC 正是从 OTCC 的源代码出发编写而成的<sup>[5](https://bellard.org/otcc/)</sup>。

## 设计

TCC 是一款单遍编译器，直接生成二进制代码，不依赖外部汇编器或链接器。Links 浏览器项目的编译速度测试发现，在 x86 平台上，TCC 的速度约为 GCC 的九倍（含汇编和链接），在 ARM 平台上则为 GCC 的 12 倍或更多<sup>[3](https://www.complang.tuwien.ac.at/Diplomarbeiten/falbesoner14.pdf)</sup>。

参考文档中的开发者指南描述了其内部结构。解析器为硬编码实现，没有 yacc 文法，且只做一遍扫描，但有两个例外：第一遍为未知大小的初始化数组统计元素个数，以及在参数按逆序求值的架构上第一遍反转参数顺序。类型存储在单个 `int` 变量中——这是编译器还简单得多时做出的选择；基本类型、`const` 和 `volatile` 等修饰符以及对其他类型的引用都被打包进该整数的各个位中<sup>[6](https://bellard.org/tcc/tcc-doc.html)</sup>。

TCC 直接输出可重定位的 ELF 目标文件、可执行 ELF 文件和动态 ELF 库，在 Windows 上则生成 PE 可执行文件和 DLL；`-oformat=` 选项可在 elf32/elf64、PE 和二进制输出格式之间选择<sup>[6](https://bellard.org/tcc/tcc-doc.html)</sup><sup> • </sup><sup>[2](https://github.com/TinyCC/tinycc/blob/mob/tcc.c)</sup>。它还支持 GNU 链接器脚本语法的一个子集，以满足 Linux 上的复杂链接需求<sup>[7](https://deepwiki.com/TinyCC/tinycc/3.1-x86-and-x86_64-support)</sup>。

## 脚本化与动态代码生成

**C 作为脚本语言。** 使用 `-run` 开关时，TCC 将给定的 C 文件编译、汇编并链接到内存中然后执行，因此首行为 `#!/usr/local/bin/tcc -run` 的 C 源文件可以像 Perl 或 shell 脚本一样直接从命令行启动，参数如同 [ANSI C](https://www.edgepedia.org/zh/ansi-c) 中那样通过 `argc` 和 `argv` 传入<sup>[3](https://www.complang.tuwien.ac.at/Diplomarbeiten/falbesoner14.pdf)</sup><sup> • </sup><sup>[1](https://github.com/TinyCC/tinycc/blob/mob/README)</sup>。文档指出，编译速度足够快，脚本运行起来就如同可执行文件一般<sup>[6](https://bellard.org/tcc/tcc-doc.html)</sup>。

**libtcc。** 编译器的核心同时也是一个库。`libtcc` 让程序可以将 TCC 用作动态代码生成的后端：包含程序的 C 字符串被传给该库，编译后其全局函数和变量即可按符号访问<sup>[6](https://bellard.org/tcc/tcc-doc.html)</sup>。还存在面向脚本语言的绑定，例如用于 Lua 的 Luatcc<sup>[3](https://www.complang.tuwien.ac.at/Diplomarbeiten/falbesoner14.pdf)</sup>。在 Windows 上，TCC 使用导入定义文件（.def）而非导入库来链接系统 DLL<sup>[8](https://github.com/acook/tinycc/blob/master/win32/tcc-win32.txt)</sup>。

## 安全特性

TCC 包含一个可选的内存和边界检查器，通过 `-b` 启用，该选项隐含生成调试信息。经过边界检查的代码可以与标准代码自由混合，且即使使用未打补丁的库，检查也能正常工作<sup>[1](https://github.com/TinyCC/tinycc/blob/mob/README)</sup><sup> • </sup><sup>[6](https://bellard.org/tcc/tcc-doc.html)</sup>。该检查器可检测 malloc 分配数据中的越界访问、已释放内存的访问以及双重释放<sup>[6](https://bellard.org/tcc/tcc-doc.html)</sup>。相关的 `-bt[N]` 选项链接回溯（栈转储）支持，最多显示 N 层调用者<sup>[2](https://github.com/TinyCC/tinycc/blob/mob/tcc.c)</sup>。

## 标准覆盖与代码质量

TCC 实现了全部 ANSI C（C89/C90），并且除复数和虚数以及变长数组外，实现了 C99 标准，外加一些 GNU 扩展和面向 x86 的内联汇编<sup>[3](https://www.complang.tuwien.ac.at/Diplomarbeiten/falbesoner14.pdf)</sup>。README 本身对 C99 的声称有所保留，称项目正在迈向完整的 ISOC99 合规，而非直接宣称已达成<sup>[1](https://github.com/TinyCC/tinycc/blob/mob/README)</sup>。标准支持在开发者中仍是一个活跃话题：2025 年 8 月 tinycc-devel 邮件列表上的一个讨论串辩论了 1.0 版本应以 C11 为目标还是留在 C99，一位参与者指出了 TCC 无法编译或编译方式与 GCC 不同的代码库<sup>[9](https://lists.gnu.org/archive/html/tinycc-devel/2025-08/msg00037.html)</sup>。

速度是以优化为代价换来的。由于其代码生成方式，TCC 不生成优化代码；要做到这一点，需要额外的中间表示以及数据流分析和寄存器分配，而一个关于 TCC 的学位论文项目设定了相互冲突的目标：在保持快速编译的同时提升代码质量<sup>[3](https://www.complang.tuwien.ac.at/Diplomarbeiten/falbesoner14.pdf)</sup>。

作为构建工具，该编译器也有实际限制。unpins 打包项目报告称，tcc 无法引导 musl libc，因为它缺少 `_Complex`，且内联汇编和约束支持仅为部分实现，其归档读取器也只解析 GNU 风格的 `ar` 索引<sup>[10](https://github.com/unpins/tcc/blob/main/README.md)</sup>。

## TCCBOOT 与引导

TCCBOOT 是一个基于 TinyCC 0.9.21 构建的引导加载程序，在启动时直接从 [Linux 内核](https://www.edgepedia.org/zh/linux-kernel)的 C 和汇编源代码编译并引导内核。它是 138 KB 的未压缩代码，在 2.4 GHz 奔腾 4 上编译并运行一个典型的 Linux 内核用时不到 15 秒；演示使用的是 Linux 2.4.26，并打了一个补丁以绕过不支持的汇编伪指令、缺失的 long long 位域及其他缺陷<sup>[11](https://textsynth.org/tcc/tccboot.html)</sup><sup> • </sup><sup>[12](https://github.com/8l/tccboot)</sup>。LWN 关于这项工作的公告标题为“TCCBOOT 在 15 秒内编译并引导 Linux 内核”<sup>[13](https://prodcs.lwn.net/Articles/108341/)</sup>。

这一特性使 TinyCC 在引导流水线中很有用——在最小种子编译器和完整工具链之间，一个小巧且可审计的编译器可作为中间步骤；2026 年 LWN 一篇关于可引导构建的讨论将 tinycc 描述为此类链条中初始编译器与 GCC 之间的垫脚石<sup>[14](https://lwn.net/Articles/1089389/)</sup>。

## 项目状态

规范仓库托管在 repo.or.cz 上，被描述为 Tiny C 编译器的当前工作仓库；最后一个标记版本是 2017 年 12 月的 0.9.27，而 0.9.26 的发布大约在那次抓取的十三年前<sup>[4](https://www.repo.or.cz/tinycc.git)</sup>。GitHub 组织 TinyCC 托管着 `mob` 开发分支的非官方镜像，这是一个约 3,000 星、采用 LGPL-2.1 许可证的 C 代码库<sup>[15](https://github.com/TinyCC)</sup>。开发在该分支上持续进行，而非通过频繁发布；项目的讨论和缺陷报告通过 tinycc-devel 邮件列表进行<sup>[2](https://github.com/TinyCC/tinycc/blob/mob/tcc.c)</sup>。

## References

1. [TinyCC/tinycc README (mob branch), GitHub](https://github.com/TinyCC/tinycc/blob/mob/README)
2. [tinycc/tcc.c (mob branch), GitHub](https://github.com/TinyCC/tinycc/blob/mob/tcc.c)
3. [Falbesoner, "Register Allocation for the Tiny C Compiler," Diplomarbeit, TU Wien](https://www.complang.tuwien.ac.at/Diplomarbeiten/falbesoner14.pdf)
4. [Public Git Hosting - tinycc.git/summary, repo.or.cz](https://www.repo.or.cz/tinycc.git)
5. [OTCC: Obfuscated Tiny C Compiler, Fabrice Bellard](https://bellard.org/otcc/)
6. [Tiny C Compiler Reference Documentation, bellard.org](https://bellard.org/tcc/tcc-doc.html)
7. [ELF Format Support, DeepWiki: TinyCC/tinycc](https://deepwiki.com/TinyCC/tinycc/3.1-x86-and-x86_64-support)
8. [tcc-win32.txt, acook/tinycc, GitHub](https://github.com/acook/tinycc/blob/master/win32/tcc-win32.txt)
9. [tinycc-devel mailing list, "VERSION Number 1.0 - C11 vs. C99," August 2025](https://lists.gnu.org/archive/html/tinycc-devel/2025-08/msg00037.html)
10. [unpins/tcc README, GitHub](https://github.com/unpins/tcc/blob/main/README.md)
11. [TCCBOOT: TinyCC Boot Loader, bellard.org](https://textsynth.org/tcc/tccboot.html)
12. [8l/tccboot: TCCBOOT repository, GitHub](https://github.com/8l/tccboot)
13. ["TCCBOOT compiles and boots a Linux kernel in 15 seconds," LWN.net](https://prodcs.lwn.net/Articles/108341/)
14. ["Bootstrappable builds: how and why" (comment thread), LWN.net](https://lwn.net/Articles/1089389/)
15. [TinyCC organization, GitHub](https://github.com/TinyCC)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Compilers, interpreters, and toolchains › Compiler implementations

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/tinycc

English original: https://www.edgepedia.org/tinycc
