# Emscripten — LLVM-to-WebAssembly Compiler for C/C++ > Emscripten compiles C and C++ code to WebAssembly, enabling high-performance applications to run in web browsers without plugins. It provides a complete toolchain including a drop-in replacement for GCC/Clang, an SDL-based graphics layer, and POSIX-compatible filesystem emulation. ## Install Save as a script file and run: # Emscripten — LLVM-to-WebAssembly Compiler for C/C++ ## Quick Use ```bash # Install via emsdk git clone https://github.com/emscripten-core/emsdk.git cd emsdk && ./emsdk install latest && ./emsdk activate latest source ./emsdk_env.sh # Compile a C file to WebAssembly emcc hello.c -o hello.html ``` ## Introduction Emscripten is an open-source compiler toolchain that converts C and C++ code into WebAssembly (WASM), allowing native-speed applications to run directly in web browsers. Originally created by Alon Zakai at Mozilla, it has become the standard way to port existing C/C++ codebases to the web platform. ## What Emscripten Does - Compiles C/C++ source code to WebAssembly binary format using LLVM/Clang - Provides libc, libcxx, and SDL implementations targeting browser APIs - Emulates POSIX filesystem operations via an in-memory virtual filesystem - Generates JavaScript glue code to bridge WASM modules with browser APIs - Supports multithreading through Web Workers and SharedArrayBuffer ## Architecture Overview Emscripten uses Clang/LLVM as its compiler frontend and backend to produce WebAssembly bytecode. The toolchain wraps this with a system of JavaScript runtime libraries (the Emscripten runtime) that implement system calls, memory management, and I/O operations by mapping them to Web APIs. The output is a .wasm binary plus a .js loader that initializes the module in the browser or Node.js environment. ## Self-Hosting & Configuration - Install via the emsdk manager which handles versioning and activation - Requires Python 3.6+ and a recent version of Node.js for tooling - Configure compiler flags via emcc/em++ which mirror GCC/Clang options - Set EMSCRIPTEN environment variable or use emsdk_env.sh for path setup - Supports CMake via the Emscripten.cmake toolchain file ## Key Features - Near-native execution speed through WebAssembly compilation - Broad POSIX API coverage including pthreads and networking - Built-in support for OpenGL ES 2.0/3.0 mapped to WebGL - Source maps for debugging C/C++ directly in browser DevTools - Asyncify transform for integrating synchronous C code with async web APIs ## Comparison with Similar Tools - **wasm-pack** — targets Rust-to-WASM; Emscripten focuses on C/C++ - **AssemblyScript** — compiles TypeScript-like syntax to WASM; less suitable for existing C codebases - **Cheerp** — similar C++ to WASM compiler but with less community adoption - **wasi-sdk** — produces standalone WASM without browser integration; Emscripten bundles full browser runtime ## FAQ **Q: Can Emscripten compile any C/C++ project?** A: Most projects compile with minor modifications. The main limitations involve platform-specific system calls and hardware access not available in browsers. **Q: How does performance compare to native code?** A: WebAssembly produced by Emscripten typically runs at 60-90% of native speed depending on the workload and browser engine optimizations. **Q: Does it support C++ exceptions?** A: Yes, Emscripten supports both C++ exceptions and setjmp/longjmp via WebAssembly exception handling or JavaScript-based unwinding. **Q: Can I use Emscripten output in Node.js?** A: Yes, the generated WASM and JS glue code work in Node.js environments without modification. ## Sources - https://github.com/emscripten-core/emscripten - https://emscripten.org/docs/ --- Source: https://tokrepo.com/en/workflows/asset-cd2a3f87 Author: Script Depot