Building Golem Components in C
Building Golem components written in C involves a few steps.
If the project was created with golem-cli new, it already has a Makefile that incorporates all the necessary steps to build the component, so it is enough to run:
$ make buildIn 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.wasmPackage 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.wasmNote 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)