Building Golem Components in C
Building Golem components written in C as it is described on the Building Components page involves three steps:
Generate the C bindings from the WIT files
$ wit-bindgen c --autodrop-borrows yes ./wit
Generating "example.c"
Generating "example.h"
Generating "example_component_type.o"
Compile the C code
Include all your source files and additionally the generated .c
and .o
files and use the clang
provided as part of the WASM SDK:
$ ~/wasi-sdk-23.0/bin/clang --sysroot ~/wasi-sdk-23.0/share/wasi-sysroot main.c example.c example_component_type.o -o example.module.wasm
Package it into a WASM component
The resulting example.module.wasm
is a WebAssembly module, not a component. To be able to use it as a Golem component, use wasm-tools
to package the module as a component:
$ wasm-tools component new example.module.wasm -o example.wasm --adapt adapters/tier1/wasi_snapshot_preview1.wasm
Note that the adapters/tier1/wasi_snapshot_preview1.wasm
file is placed in the project's directory when using golem-cli new
to create the new project.
If needed, it can be manually downloaded from https://github.com/golemcloud/golem-wit/blob/main/adapters/tier1/wasi_snapshot_preview1.wasm (opens in a new tab)