About Emscripten
Emscripten is a completeOpen Source compilertoolchain 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 thenrun code in those other languages in anindirect way (for example, this hasbeen done forPython andLua).
Practically anyportable C or C++ codebase can be compiled into WebAssemblyusing Emscripten, ranging from high-performance games that need to rendergraphics, play sounds, and load and process files, through to applicationframeworks like Qt. Emscripten has already been used to convert a very long listof real-world codebases to WebAssembly, including commercial codebases like theUnreal Engine 4and theUnity engine.For examples and demos, see thecommunity-maintained list on the wiki.
Emscripten generates small and fast code! Its default output format isWebAssembly , a highly optimizable executableformat, that runs almost as fast as native code, while being portable and safe.Emscripten does a lot of optimization work for you automatically, by carefulintegration with LLVM,Binaryen,Closure Compiler, and othertools.
Emscripten Toolchain
A high level view of the Emscripten toolchain is given below. The main tool istheEmscripten Compiler Frontend (emcc). This is a drop-in replacement for a standard compiler likegcc orclang.
Emcc usesClang and LLVM to compile to WebAssembly. Emcc alsoemits JavaScript that provides API support to the compiled code. That JavaScriptcan be executed byNode.js, or from within HTML in a browser.
TheEmscripten SDK is used to install the entire toolchain, including emcc andLLVM and so forth. TheEmscripten SDK (emsdk) can be used on Linux, Windows or MacOS.
Porting code to use Emscripten
Emscripten support forportable C/C++ code is fairly comprehensive.Support for the C standard library, C++ standard library, C++ exceptions, etc.is very good, as isSDL2 and other APIs.OpenGL support in Emscriptensupport is excellent for OpenGL ES 2.0-type code, and acceptable for other types.
There are differences between the native andEmscripten Runtime Environment,which mean some changes usually need to be made to the native code. That said,many applications will only need to change the way they define their main loop,and also modify theirfile handling to adapt tothe limitations of the browser/JavaScript.
There are also limitations that can make some code easier to port — readPortability Guidelines to determine where you may need to spend moreeffort.
