LinuxCommandLibrary

emcc

TLDR

Compile C to WebAssembly

$ emcc [input.c] -o [output.wasm]
copy
Compile to JavaScript
$ emcc [input.c] -o [output.js]
copy
Compile with HTML harness
$ emcc [input.c] -o [output.html]
copy
Compile C++
$ em++ [input.cpp] -o [output.js]
copy
Optimize for size
$ emcc -Os [input.c] -o [output.wasm]
copy
Optimize for speed
$ emcc -O3 [input.c] -o [output.wasm]
copy
Export specific functions
$ emcc -sEXPORTED_FUNCTIONS=['_main','_myFunc'] [input.c] -o [output.js]
copy

SYNOPSIS

emcc [options] files

DESCRIPTION

emcc (Emscripten Compiler Frontend) compiles C and C++ code to WebAssembly (Wasm) or JavaScript using LLVM and Emscripten. It enables running native code in web browsers and other Wasm runtimes.
Emscripten provides a compatibility layer for POSIX APIs, OpenGL (via WebGL), and SDL. It can compile entire codebases including games and applications to run in browsers.

PARAMETERS

-o file

Output file (.js, .wasm, .html).
-O0 to -O3, -Os, -Oz
Optimization levels.
-s SETTING=VALUE
Emscripten-specific settings.
-I dir
Include directory.
-L dir
Library directory.
-l lib
Link library.
-g
Generate debug information.
--preload-file path
Preload file into virtual filesystem.
--embed-file path
Embed file in output.
-sMODULARIZE
Output as ES6 module.

CAVEATS

Not all C/C++ features work identically. File system access requires virtual filesystem. Threading needs SharedArrayBuffer. Performance differs from native. Large applications may have long compile times. Browser security restrictions apply.

HISTORY

Emscripten was created by Alon Zakai at Mozilla in 2011, initially targeting asm.js before WebAssembly existed. When WebAssembly launched in 2017, Emscripten became the primary compiler toolchain for bringing C/C++ to the web.

SEE ALSO

clang(1), wasm-ld(1), wasm2wat(1)

Copied to clipboard