emcc: Compile C/C++ to Wasm with Emscripten
emcc is the Emscripten compiler front end that builds C/C++ into wasm together with JavaScript glue for the browser or Node.
Emscripten targets the web: emcc emits a .wasm plus a .js loader (and optional .html). In CI you activate emsdk first, then compile, then run under Node.
What it does
emcc compiles and links C/C++ to wasm. The output extension decides the shape: -o out.js emits wasm plus a JS loader, -o out.html adds a page, and -o out.wasm (with -s STANDALONE_WASM) emits a bare module. Runtime behavior is tuned with -s settings.
Common usage
emcc hello.c -o hello.js
emcc hello.c -O2 -o hello.html
emcc hello.c -s STANDALONE_WASM -o hello.wasm
emcc a.c b.c -s EXPORTED_FUNCTIONS=_main,_add -o out.jsOptions
| Flag | What it does |
|---|---|
| -o <file.js|.html|.wasm> | Output; extension selects the artifact shape |
| -O0..-O3 / -Os / -Oz | Optimization level |
| -s <SETTING>=<value> | Emscripten setting, e.g. -s STANDALONE_WASM |
| -s EXPORTED_FUNCTIONS=... | C functions to export (prefix with _) |
| --preload-file <path> | Bundle files into the virtual filesystem |
In CI
Activate emsdk before compiling: source ./emsdk_env.sh after emsdk install latest && emsdk activate latest. emsdk is large; cache the install directory. Pin the emsdk version so output and settings stay stable.
Common errors in CI
"emcc: command not found" means emsdk is not installed or its env was not sourced (run source emsdk_env.sh). "error: undefined symbol: ..." (from wasm-ld) means a missing library or export. "CMake Error ... Emscripten toolchain" means the emcmake/toolchain file was not used. First-run cache builds can be slow; cache ~/.emscripten_cache.