Top Related Projects
The WebAssembly Binary Toolkit
A fast and secure runtime for WebAssembly
🚀 The leading Wasm Runtime supporting WASIX, WASI and Emscripten
A TypeScript-like language for WebAssembly.
📦✨ your favorite rust -> wasm workflow tool!
WebAssembly Micro Runtime (WAMR)
Quick Overview
Emscripten is a complete compiler toolchain that allows you to compile C and C++ code to WebAssembly, enabling you to run high-performance code in web browsers. It provides a robust set of tools and APIs to facilitate the porting of native applications to the web platform.
Pros
- Enables running C/C++ code in web browsers at near-native speed
- Provides a comprehensive set of tools and APIs for porting existing applications
- Supports a wide range of standard libraries and APIs, including OpenGL and SDL
- Actively maintained with regular updates and improvements
Cons
- Learning curve can be steep for developers new to WebAssembly or C/C++
- Debugging can be challenging compared to native development
- Resulting file sizes can be large, especially for complex applications
- Some platform-specific features may not be available or require workarounds
Code Examples
- Hello World example:
#include <emscripten/emscripten.h>
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
- Using Emscripten's JavaScript interop:
#include <emscripten/emscripten.h>
#include <emscripten/bind.h>
int add(int a, int b) {
return a + b;
}
EMSCRIPTEN_BINDINGS(my_module) {
emscripten::function("add", &add);
}
- Using Emscripten's SDL support:
#include <SDL2/SDL.h>
#include <emscripten.h>
SDL_Window *window;
SDL_Renderer *renderer;
void mainloop() {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
int main() {
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(640, 480, 0, &window, &renderer);
emscripten_set_main_loop(mainloop, 0, 1);
return 0;
}
Getting Started
-
Install Emscripten:
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.cpp -o hello.html
-
Serve the resulting files using a web server and open the HTML file in a browser to run your WebAssembly application.
Competitor Comparisons
The WebAssembly Binary Toolkit
Pros of wabt
- Lightweight and focused specifically on WebAssembly tooling
- Provides a suite of command-line tools for working with WebAssembly binaries
- Faster compilation times for simple WebAssembly projects
Cons of wabt
- Limited high-level language support compared to Emscripten
- Lacks the extensive runtime and library support offered by Emscripten
- Requires more manual configuration for complex projects
Code Comparison
wabt (wat2wasm):
(module
(func $add (param $a i32) (param $b i32) (result i32)
local.get $a
local.get $b
i32.add)
(export "add" (func $add)))
Emscripten:
#include <emscripten.h>
EMSCRIPTEN_KEEPALIVE
int add(int a, int b) {
return a + b;
}
wabt focuses on low-level WebAssembly manipulation, while Emscripten provides a more comprehensive toolchain for compiling C/C++ to WebAssembly. wabt is ideal for WebAssembly-specific tasks, whereas Emscripten offers a broader ecosystem for web development with WebAssembly.
A fast and secure runtime for WebAssembly
Pros of Wasmtime
- Faster execution speed for WebAssembly modules
- More flexible runtime environment, supporting various languages and platforms
- Better support for WASI (WebAssembly System Interface) integration
Cons of Wasmtime
- Steeper learning curve for developers new to WebAssembly
- Less comprehensive toolchain compared to Emscripten's ecosystem
- May require additional setup for certain use cases
Code Comparison
Emscripten (compiling C++ to WebAssembly):
#include <emscripten.h>
#include <iostream>
EMSCRIPTEN_KEEPALIVE
extern "C" int add(int a, int b) {
return a + b;
}
Wasmtime (running WebAssembly module in Rust):
use wasmtime::*;
let engine = Engine::default();
let module = Module::from_file(&engine, "add.wasm")?;
let instance = Instance::new(&mut store, &module, &[])?;
let add = instance.get_func(&mut store, "add")?;
let result = add.call(&mut store, &[Val::I32(5), Val::I32(37)])?;
This comparison highlights the different approaches: Emscripten focuses on compiling C/C++ to WebAssembly, while Wasmtime provides a runtime for executing WebAssembly modules in various host environments.
🚀 The leading Wasm Runtime supporting WASIX, WASI and Emscripten
Pros of Wasmer
- Supports multiple programming languages, not limited to C/C++
- Can run WebAssembly modules outside the browser environment
- Provides a standalone runtime for WebAssembly
Cons of Wasmer
- Less mature ecosystem compared to Emscripten
- May have limited support for certain C/C++ features or libraries
- Potentially slower compilation times for large projects
Code Comparison
Emscripten (compiling C to WebAssembly):
#include <emscripten.h>
#include <stdio.h>
EMSCRIPTEN_KEEPALIVE
int add(int a, int b) {
return a + b;
}
Wasmer (running WebAssembly in Rust):
use wasmer::{imports, Instance, Module, Store};
let module = Module::from_file(&store, "add.wasm")?;
let instance = Instance::new(&module, &imports! {})?;
let add = instance.exports.get_function("add")?;
let result = add.call(&[Value::I32(5), Value::I32(37)])?;
This comparison highlights the different approaches: Emscripten focuses on compiling C/C++ to WebAssembly, while Wasmer provides a runtime for executing WebAssembly modules in various environments and languages.
A TypeScript-like language for WebAssembly.
Pros of AssemblyScript
- Easier learning curve for TypeScript developers
- Smaller output size for simple programs
- Better integration with existing TypeScript/JavaScript toolchains
Cons of AssemblyScript
- Less mature ecosystem and community support
- Limited support for complex C/C++ libraries
- Fewer optimization options compared to Emscripten
Code Comparison
AssemblyScript:
export function add(a: i32, b: i32): i32 {
return a + b;
}
Emscripten:
#include <emscripten.h>
EMSCRIPTEN_KEEPALIVE
extern "C" int add(int a, int b) {
return a + b;
}
AssemblyScript provides a more familiar syntax for JavaScript developers, while Emscripten allows direct compilation of C/C++ code to WebAssembly. AssemblyScript's type system is based on TypeScript, making it easier for web developers to adopt. However, Emscripten offers broader compatibility with existing C/C++ codebases and libraries.
Emscripten has a more extensive set of features and optimizations, making it suitable for complex projects. AssemblyScript, on the other hand, excels in simplicity and ease of use for smaller projects or when integrating with existing TypeScript/JavaScript workflows.
Both tools have their strengths, and the choice between them depends on the specific requirements of your project, your team's expertise, and the existing codebase you're working with.
📦✨ your favorite rust -> wasm workflow tool!
Pros of wasm-pack
- Specifically designed for Rust-to-WebAssembly compilation, offering a streamlined workflow
- Integrates well with npm, making it easier to publish and distribute Rust-based WebAssembly modules
- Provides built-in testing and documentation generation for WebAssembly modules
Cons of wasm-pack
- Limited to Rust language, whereas Emscripten supports multiple languages (C, C++)
- Less mature ecosystem compared to Emscripten, which has been around longer
- May have fewer optimization options for WebAssembly output compared to Emscripten
Code Comparison
wasm-pack:
#[wasm_bindgen]
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
Emscripten:
#include <emscripten.h>
EMSCRIPTEN_KEEPALIVE
int add(int a, int b) {
return a + b;
}
Both examples show a simple function that adds two integers, demonstrating the syntax differences between Rust (wasm-pack) and C (Emscripten) for creating WebAssembly-compatible functions. wasm-pack uses the wasm_bindgen
attribute, while Emscripten uses the EMSCRIPTEN_KEEPALIVE
macro to ensure the function is exported to WebAssembly.
WebAssembly Micro Runtime (WAMR)
Pros of wasm-micro-runtime
- Lightweight and efficient, suitable for resource-constrained environments
- Supports multiple architectures and platforms, including IoT devices
- Provides a standalone WebAssembly runtime without browser dependencies
Cons of wasm-micro-runtime
- Limited language support compared to Emscripten (primarily C/C++)
- Smaller ecosystem and community support
- Fewer built-in tools and utilities for development and optimization
Code Comparison
Emscripten:
#include <emscripten.h>
#include <stdio.h>
EMSCRIPTEN_KEEPALIVE
int add(int a, int b) {
return a + b;
}
wasm-micro-runtime:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
printf("Result: %d\n", add(2, 3));
return 0;
}
The main difference in the code examples is that Emscripten requires the EMSCRIPTEN_KEEPALIVE
macro to export functions, while wasm-micro-runtime doesn't need special annotations. Emscripten also provides additional APIs for interacting with the JavaScript environment, which are not present in wasm-micro-runtime.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Main project page: https://emscripten.org
Chromium builder status: emscripten-releases
Overview
Emscripten compiles C and C++ to WebAssembly using LLVM and Binaryen. Emscripten output can run on the Web, in Node.js, and in wasm runtimes.
Emscripten provides Web support for popular portable APIs such as OpenGL and SDL2, allowing complex graphical native applications to be ported, such as the Unity game engine and Google Earth. It can probably port your codebase, too!
While Emscripten mostly focuses on compiling C and C++ using
Clang, it can be integrated with other LLVM-using
compilers (for example, Rust has Emscripten integration, with the
wasm32-unknown-emscripten
and asmjs-unknown-emscripten
targets).
License
Emscripten is available under 2 licenses, the MIT license and the University of Illinois/NCSA Open Source License.
Both are permissive open source licenses, with little if any practical difference between them.
The reason for offering both is that (1) the MIT license is well-known and suitable for a compiler toolchain, while (2) LLVM's original license, the University of Illinois/NCSA Open Source License, was also offered to allow Emscripten's code to be integrated upstream into LLVM. The second reason became less important after Emscripten switched to the LLVM wasm backend, at which point there isn't any code we expect to move back and forth between the projects; also, LLVM relicensed to Apache 2.0 + exceptions meanwhile. In practice you can just consider Emscripten as MIT licensed (which allows you to do pretty much anything you want with a compiler, including commercial and non-commercial use).
See LICENSE
for the full content of the licenses.
Top Related Projects
The WebAssembly Binary Toolkit
A fast and secure runtime for WebAssembly
🚀 The leading Wasm Runtime supporting WASIX, WASI and Emscripten
A TypeScript-like language for WebAssembly.
📦✨ your favorite rust -> wasm workflow tool!
WebAssembly Micro Runtime (WAMR)
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot