Building Golem Components in C
Building Golem components having an application manifest is straightforward, just use the golem
command line interface:
golem app build
If the project was created using golem app new
as recommended, the golem app build
command will always work as expected.
The result of the golem app build
command is a WebAssembly component file ready to be uploaded to Golem. It does not have to be specified explicitly, as the golem
tool will automatically find the correct file when doing for example:
golem component add
Under the hood
Under the hood, building Golem components written in C involves a few steps.
In details, building the component requires the following 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_PATH/bin/clang --sysroot $WASI_SDK_PATH/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 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)