# Emscripten

**Emscripten** 是一个开源编译器工具链，可将 C 和 C++ 代码，或任何使用 LLVM 的其他语言，编译为 [WebAssembly](https://www.edgepedia.org/zh/webassembly)，以便在 Web、Node.js 或其他 Wasm 运行时中运行。<sup>[1](https://emscripten.org/docs/introducing_emscripten/about_emscripten)</sup> 它由阿隆·扎凯（Alon Zakai）创建，最初只是他为了避免用 [JavaScript](https://www.edgepedia.org/zh/javascript) 重写一款 C++ 游戏而做的业余项目，后来在 Mozilla 的游戏项目中得到开发。<sup>[2](https://spectrum.ieee.org/webassembly-will-finally-let-you-run-highperformance-applications-in-your-browser)</sup>

| 关键事实 | 详情 |
|---|---|
| 是什么 | 一个面向 WebAssembly 的完整开源编译器工具链；主工具 emcc 可直接替换 gcc 或 clang 等标准编译器<sup>[1](https://emscripten.org/docs/introducing_emscripten/about_emscripten)</sup> |
| 起源 | 由阿隆·扎凯创建；名称结合了 JavaScript 中的“script”和《辛普森一家》中的“embiggen”；开发历时两年，先是业余项目，后在 Mozilla 的游戏项目中推进<sup>[2](https://spectrum.ieee.org/webassembly-will-finally-let-you-run-highperformance-applications-in-your-browser)</sup> |
| 后端历史 | 自 1.39.0 版（2019 年 10 月）起使用上游 LLVM Wasm 后端生成 WebAssembly；旧的 fastcomp 后端在 2.0.0 版（2020 年 8 月）中被移除<sup>[3](https://emscripten.org/docs/compiling/WebAssembly.html?highlight=wasmBinary)</sup> |
| 安装 | Emscripten SDK（emsdk）可在 Linux、Windows 或 macOS 上安装整个工具链，包括 emcc 和 LLVM<sup>[1](https://emscripten.org/docs/introducing_emscripten/about_emscripten)</sup> |
| 已知用途 | 转换为 WebAssembly 的真实代码库包括虚幻引擎 4 和 Unity 引擎<sup>[1](https://emscripten.org/docs/introducing_emscripten/about_emscripten)</sup> |
| 性能 | 以往研究报告称，许多编译为 WebAssembly 的应用平均比原生代码慢 10%<sup>[4](https://arxiv.org/abs/1901.09056)</sup> |

## 工具链如何工作

Emscripten 编译器前端（emcc）使用 Clang 和 LLVM 编译为 WebAssembly，同时生成 JavaScript，为编译后的代码提供 API 支持。<sup>[1](https://emscripten.org/docs/introducing_emscripten/about_emscripten)</sup> Emscripten 与 LLVM、Binaryen（一个用于修改和优化 Wasm 的 WebAssembly 编译器工具包）、Closure Compiler 及其他工具集成，自动执行优化。<sup>[1](https://emscripten.org/docs/introducing_emscripten/about_emscripten)</sup><sup> • </sup><sup>[5](https://emscripten.org/docs/site/glossary.html)</sup> Emscripten SDK 安装整个工具链，包括 emcc 和 LLVM。<sup>[1](https://emscripten.org/docs/introducing_emscripten/about_emscripten)</sup>

**后端历史。** Emscripten 比 WebAssembly 本身出现得更早。它最初的输出是 asm.js，即 JavaScript 的一个底层、可优化的子集；Mozilla 的工程师发现生成的 JavaScript 中隐藏着一套类型系统，并让它运行得很快，这个子集被命名为 asm.js。<sup>[6](https://hacks.mozilla.org/2018/10/webassemblys-post-mvp-future/)</sup> 在 WebAssembly 设计的早期阶段，Emscripten 将 C 和 C++ 编译为 asm.js，再由 Binaryen 的 asm2wasm 工具将 asm.js 转换为 WebAssembly，从而在浏览器能够执行 Wasm 之前就提供了一条完整的 C/C++ 到 WebAssembly 的路径。<sup>[7](https://hacks.mozilla.org/2015/12/compiling-to-webassembly-its-happening/)</sup> 自 1.39.0 版（2019 年 10 月）起，Emscripten 使用上游 LLVM Wasm 后端生成 WebAssembly；旧的 fastcomp 后端在 2.0.0 版（2020 年 8 月）中被移除。<sup>[3](https://emscripten.org/docs/compiling/WebAssembly.html?highlight=wasmBinary)</sup> 默认生成 WebAssembly，编译为 Wasm 还是 JavaScript 可在链接阶段决定，不影响目标文件。<sup>[3](https://emscripten.org/docs/compiling/WebAssembly.html?highlight=wasmBinary)</sup>

## 移植代码

Emscripten 对可移植 C/C++ 的支持范围很广：C 标准库、C++ 标准库和 C++ 异常被描述为支持得非常好，SDL2 及其他 API 也是如此。OpenGL 支持对于 [OpenGL ES](https://www.edgepedia.org/zh/opengl-es) 2.0 类型的代码被描述为极佳，对其他类型也可接受。<sup>[1](https://emscripten.org/docs/introducing_emscripten/about_emscripten)</sup> 对于基于 SDL 的游戏，Emscripten 附带一个移植层，将窗口映射到 canvas，渲染映射到 WebGL，输入事件映射到浏览器事件，音频映射到 Web Audio 系统。<sup>[8](https://abratabia.com/webassembly/cpp-games-in-browser.php)</sup> 许多应用只需改变主循环的定义方式，并调整文件处理以适应浏览器的限制。<sup>[1](https://emscripten.org/docs/introducing_emscripten/about_emscripten)</sup>

有些代码完全无法编译。Emscripten 编译的代码要求小端主机——文档指出这涵盖了接入互联网的 99% 的机器——因为 JavaScript 类型化数组遵循主机的字节序。<sup>[9](https://emscripten.org/docs/porting/guidelines/portability_guidelines.html?highlight=exception)</sup> 架构相关的内联汇编、扫描寄存器或栈的代码，以及用 `setjmp`/`longjmp` 跳上已展开栈的原生栈操作，都必须重写。<sup>[9](https://emscripten.org/docs/porting/guidelines/portability_guidelines.html?highlight=exception)</sup> 另一些代码可以编译但运行缓慢：C++ 异常通常会让 JavaScript 引擎关闭优化，因此在 `-O1` 及以上级别默认禁用异常，可通过 `-sDISABLE_EXCEPTION_CATCHING=0` 重新启用。<sup>[9](https://emscripten.org/docs/porting/guidelines/portability_guidelines.html?highlight=exception)</sup>

**线程。** Emscripten 通过在 Web Worker 中运行来支持 [POSIX 线程](https://www.edgepedia.org/zh/posix-threads)，但 Web atomics API 不允许在主浏览器线程上阻塞（`Atomics.wait` 在那里不起作用），因此 `pthread_join` 等阻塞调用用忙等待模拟，可能导致标签页无响应。因此，当 `pthread_join` 或 `pthread_cond_wait` 发生在主浏览器线程上时，Emscripten 默认发出警告，而 `PROXY_TO_PTHREAD` 选项将 `main()` 移到 pthread 上，使主浏览器线程只接收代理事件。<sup>[10](https://emscripten.org/docs/porting/pthreads.html)</sup> 不支持 POSIX 信号，因为信号无法发送给 Web Worker；`pthread_kill()` 是唯一的例外。基于 `fork()` 的多进程也不受支持。<sup>[10](https://emscripten.org/docs/porting/pthreads.html)</sup> 默认分配器 dlmalloc 有一个全局锁；mimalloc 分配器可通过 `-sMALLOC=mimalloc` 选择，每个线程有独立的分配上下文，在 `malloc`/`free` 争用下扩展性更好。<sup>[10](https://emscripten.org/docs/porting/pthreads.html)</sup>

## 生态定位与质量

Emscripten 是研究文献中研究的两个主要 C 到 WebAssembly 编译器之一，另一个是 Cheerp；两者都使用 LLVM 后端，但 Emscripten 的 JavaScript 回退是 asm.js，而非标准 JavaScript。<sup>[11](https://weihang-wang.github.io/papers/imc21.pdf)</sup> 它与 WASI SDK 工具链不同，后者面向非浏览器环境的 [WebAssembly 系统接口](https://www.edgepedia.org/zh/webassembly-system-interface)；一篇比较论文指出 Emscripten 在某些方面支持更多，而一份基准测试报告观察到，尽管 Emscripten 总体上支持 WASI，但在撰写之时，读写 WASI 文件描述符仅针对 STDOUT 和 STDERR 实现。<sup>[12](https://kth.diva-portal.org/smash/get/diva2%3A1763085/FULLTEXT01.pdf)</sup><sup> • </sup><sup>[13](https://www.hb.fh-muenster.de/opus4/frontdoor/deliver/index/docId/15523/file/Abschlussbericht_FEP_2022.pdf)</sup>

交叉编译遗留代码存在风险。一项对 115 个开源代码库的研究指出了将遗留 C/C++ 交叉编译为 WebAssembly 的关键挑战，包括静默误编译的风险，并在 Emscripten 编译器工具链中发现了 11 个新缺陷，均得到 Emscripten 开发者确认。<sup>[14](https://arxiv.org/abs/2412.20258)</sup>

**近期版本。** 在 6.0.10 中，`WASM=0` 和 `WASM=2`（wasm2js）JavaScript 输出模式被标记为弃用，SDL3 移植不再被视为实验性，mimalloc 更新至 3.5.1。<sup>[15](https://newreleases.io/project/github/emscripten-core/emscripten/release/6.0.10)</sup>

## References

1. [About Emscripten, Emscripten documentation](https://emscripten.org/docs/introducing_emscripten/about_emscripten)
2. [WebAssembly Will Finally Let You Run High-Performance Applications in Your Browser, IEEE Spectrum](https://spectrum.ieee.org/webassembly-will-finally-let-you-run-highperformance-applications-in-your-browser)
3. [Building to WebAssembly, Emscripten documentation](https://emscripten.org/docs/compiling/WebAssembly.html?highlight=wasmBinary)
4. [Not So Fast: Analyzing the Performance of WebAssembly vs. Native Code, arXiv](https://arxiv.org/abs/1901.09056)
5. [Glossary, Emscripten documentation](https://emscripten.org/docs/site/glossary.html)
6. [WebAssembly's post-MVP future: A cartoon skill tree, Mozilla Hacks](https://hacks.mozilla.org/2018/10/webassemblys-post-mvp-future/)
7. [Compiling to WebAssembly: It's Happening!, Mozilla Hacks](https://hacks.mozilla.org/2015/12/compiling-to-webassembly-its-happening/)
8. [Running a C++ Game in the Browser with WebAssembly, Abratabia](https://abratabia.com/webassembly/cpp-games-in-browser.php)
9. [Portability Guidelines, Emscripten documentation](https://emscripten.org/docs/porting/guidelines/portability_guidelines.html?highlight=exception)
10. [Pthreads support, Emscripten documentation](https://emscripten.org/docs/porting/pthreads.html)
11. [Understanding the Performance of WebAssembly Applications, IMC 2021](https://weihang-wang.github.io/papers/imc21.pdf)
12. [Degree Project in Computer Science and Engineering, KTH](https://kth.diva-portal.org/smash/get/diva2%3A1763085/FULLTEXT01.pdf)
13. [Performance Comparison of different WebAssembly Source Languages, FH Münster](https://www.hb.fh-muenster.de/opus4/frontdoor/deliver/index/docId/15523/file/Abschlussbericht_FEP_2022.pdf)
14. [Reusing Legacy Code in WebAssembly: Key Challenges of Cross-Compilation and Code Semantics Preservation, arXiv](https://arxiv.org/abs/2412.20258)
15. [emscripten-core/emscripten 6.0.10 release notes, NewReleases](https://newreleases.io/project/github/emscripten-core/emscripten/release/6.0.10)

---
*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/emscripten

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