Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
When compiling to WebAssembly,
There are two main ways to generate such standalone WebAssembly from Emscripten: letting the optimizer remove the runtime, or creating a dynamic library. We'll go over each on this page.
Let the optimizer remove the runtime
As of 1.37.29,
emcc source.c -Os -s WASM=1
-Os is needed to make the optimizer work at full power (you can also use
- If you use C++ objects, the compiler must in many cases emit code to catch exceptions so that RAII destructors are called, and exceptions require a lot of runtime support. Build with
-fno-exceptionsto disable exceptions entirely.
- More details on how this works.
Create a dynamic library
Dynamic libraries have a formal definition, and are designed to be loadable in a standard way. That is a benefit over the previous approach, however, dynamic libraries also have downsides, such as having relocations for memory and function pointers, which add overhead that may be unnecessary if you are only using one module (and not linking several together).
To build a dynamic library, use
emcc source.c -s WASM=1 -s SIDE_MODULE=1 -o target.wasm
That will emit the output dynamic library as
To use a side module, see
src/runtime.js for some example loading code.
- The conventions for a wasm dynamic library are here.
- Note that there is no special handling of a C stack. A module can have one internally if it wants one (it needs to ask for the memory for it, then handle it however it wants).
- Full example.
- The LLVM wasm backend does not support dynamic libraries yet, so you can only use this with asm2wasm (fastcomp).