Emscripten is a C++ compiler for Webassembly

Emscripten is a complete WebAssembly open source compiler tool chain. You can use Emscripten

  • C and C++ code or any other language using LLVM compiles to WebAssembly and runs it on the Web, Node.js, or other WASM runtimes.
  • Compile C/C++ runtimes from other languages into WebAssembly, and then run code from other languages indirectly (Python and Lua already do this, for example).
  • Virtually any portable C or C++ code base can be compiled into WebAssembly using Emscripten, from high-performance games that need to render graphics, play sound, load and process files, to application frameworks such as Qt. Emscripten has been used to convert a long list of real-world code libraries to WebAssembly, including commercial code libraries such as the Unreal Engine 4 and Unity engines. For examples and demonstrations, see the community maintenance list on the wiki.
  • Emscripten generates small, fast code! Its default output format is WebAssembly, a highly optimized executable format that runs almost as fast as native code, while being portable and secure. Emscripten automatically does a lot of optimization for you through careful integration with LLVM, Binaryen, Closure Compiler, and other tools.

Emscripten tool chain

Emscripten Compiler Frontend (EMCC). This is a direct alternative to standard compilers such as GCC or CLang.

Emcc compiles to WebAssembly using Clang and LLVM. Emcc also emits JavaScript to provide API support for compiled code. JavaScript can be executed by Node.js, or it can be executed by HTML in a browser.

This Emscripten SDK is a tutorial for installing a complete toolchain, including EMCC and LLVM, and runs on Linux, Windows, and MacOS

Migrate the code using Emscripten

Emscripten’s support for portable C/C++ code is quite comprehensive. The C library, C++ library, C++ exceptions, and so on are well supported, as are SDL2 and other apis. The OpenGL support in Emscripten support is well suited for OpenGL ES 2.0 type code and is acceptable for other types.

There are differences between the native and Emscripten runtime environments, which means that changes to the native code are often required. That said, many applications just need to change the way they define the main loop and modify their file handling to accommodate browser /JavaScript constraints.

There are also limitations that can make some code more portable — read the portability guide to determine where you might need to put more effort.

Reference

emscripten.org/index.html