wasm2c
Compile WebAssembly modules to C source code
TLDR
Convert a file to a C source file and header and display it to the console
Write the output to a given file (file.h gets additionally generated)
SYNOPSIS
wasm2c [options] <input.wasm> [-o <output_prefix>]
PARAMETERS
-o <output_prefix>
Specifies the prefix for the generated C and header files. E.g., -o my_module creates my_module.c and my_module.h.
--enable-all
Enables all experimental WebAssembly features during conversion.
--enable-<feature>
Enables a specific experimental WebAssembly feature (e.g., --enable-bulk-memory).
--disable-<feature>
Disables a specific WebAssembly feature during conversion.
--import-prefix <prefix>
Adds a specified prefix to all imported function names in the generated C code.
--no-canonicalize-floats
Disables canonicalization of float values, preserving their exact bit patterns.
--ignore-stack-overflow
Suppresses emission of stack overflow checks in the generated C code.
--source-map-url <url>
Embeds a source map URL in the generated output.
-h, --help
Displays a help message and exits.
-v, --version
Displays version information and exits.
DESCRIPTION
wasm2c is a command-line tool, part of the WebAssembly Binary Toolkit (WABT), designed to translate a WebAssembly binary module (.wasm file) into a C source file (.c file) and a corresponding header file (.h). This conversion enables developers to embed WebAssembly modules directly into C/C++ applications without requiring a full WebAssembly runtime. The generated C code includes a C API that can be called from the host application to interact with the Wasm module's exported functions and memory. This functionality is particularly valuable for ahead-of-time compilation, reducing runtime dependencies, or integrating Wasm logic into environments where a dedicated Wasm runtime might be too resource-intensive, such as embedded systems. The output C code is intended to be compiled by a standard C compiler (like GCC or Clang) and linked with the host application.
CAVEATS
The generated C code is typically not intended for human readability or direct modification; it's an output of a compilation process.
It often requires linking with a small runtime component or specific helper functions assumed or generated by wasm2c.
The C output might not achieve the same performance as a JIT-compiled WebAssembly module, as it's a direct translation rather than an optimized compilation for a specific architecture.
This tool primarily targets embedding Wasm into C/C++ applications, not as a general-purpose Wasm-to-native compiler.
TYPICAL COMPILATION WORKFLOW
The typical workflow involves using wasm2c to generate C and header files from a .wasm binary. These generated files can then be compiled using a standard C compiler like gcc or clang. The resulting object files are usually linked with a host C/C++ application that utilizes the exposed API from the generated C code to interact with the original WebAssembly module's functions and memory. This process enables the direct embedding of WebAssembly logic into native applications, offering a lightweight alternative to dynamic runtimes.
HISTORY
wasm2c is an integral part of the WebAssembly Binary Toolkit (WABT), an open-source project primarily developed and maintained by Mozilla and the broader WebAssembly community. WABT was created to provide a suite of tools for working with WebAssembly modules, facilitating tasks like conversion, inspection, and validation. wasm2c specifically emerged to support scenarios where a full WebAssembly runtime is undesirable or unavailable, offering a static compilation target for Wasm modules and enabling their integration into traditional C/C++ environments, including embedded systems and ahead-of-time compilation workflows. Its development tracks the evolution of the WebAssembly specification, with new features being added as they stabilize.
SEE ALSO
wat2wasm(1), wasm-objdump(1), wasm-interp(1), gcc(1), clang(1)