🔧 Minimal C++ to WASM Binding Example

June 28, 2025

This is a minimal example to show how a C++ function can be compiled to WebAssembly and called from JavaScript using Emscripten.

example.cpp


extern "C" int add(int a, int b) {
    return a + b;
}
    

Compilation command


emcc example.cpp -o example.js \
  -sEXPORTED_FUNCTIONS='["_add"]' \
  -sEXPORTED_RUNTIME_METHODS='["cwrap"]'
    

You can now call add from JS via Module.cwrap("add", "number", ["number", "number"]).

← Back to articles