关于Emscripten

Emscripten is a complete Open Source compiler toolchain to WebAssembly. Using Emscripten you can:

  • Compile C and C++ code, or any other language that uses LLVM, into WebAssembly, and run it on the Web, Node.js, or other Wasm runtimes.

  • Compile the C/C++ runtimes of other languages into WebAssembly, and then run code in those other languages in an indirect way (for example, this has been done for Python and Lua).

Practically any portable C or C++ codebase can be compiled into WebAssembly using Emscripten, ranging from high-performance games that need to render graphics, play sounds, and load and process files, through to application frameworks like Qt. Emscripten has already been used to convert a very long list of real-world codebases to WebAssembly, including commercial codebases like the Unreal Engine 4 and the Unity engine. For examples and demos, see the community-maintained list on the wiki.

Emscripten generates small and fast code! Its default output format is WebAssembly , a highly optimizable executable format, that runs almost as fast as native code, while being portable and safe. Emscripten does a lot of optimization work for you automatically, by careful integration with LLVM, Binaryen, Closure Compiler, and other tools.

Emscripten工具链

A high level view of the Emscripten toolchain is given below. The main tool is the Emscripten编译器前端(emcc). This is a drop-in replacement for a standard compiler like gcc or clang.

Emcc uses Clang and LLVM to compile to WebAssembly. Emcc also emits JavaScript that provides API support to the compiled code. That JavaScript can be executed by Node.js, or from within HTML in a browser.

The Emscripten SDK is used to install the entire toolchain, including emcc and LLVM and so forth. The Emscripten SDK (emsdk) can be used on Linux, Windows or MacOS.

为Emscripten移植代码

Emscripten support for portable C/C++ code is fairly comprehensive. Support for the C standard library, C++ standard library, C++ exceptions, etc. is very good, as is SDL2 and other APIs. Emscripten中的OpenGL支持 support is excellent for OpenGL ES 2.0-type code, and acceptable for other types.

本机环境和 Emscripten运行时环境 之间存在差异,这意味着通常需要对本机代码进行一些更改。也就是说,许多应用程序仅需要更改定义主循环的方式,并且修改 file handling 以适应浏览器/ JavaScript的限制。

还有一些限制可以使某些代码更易于移植-请阅读 移植指南 以确定您可能需要花更多精力的地方。