Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compiling C to WebAssembly using Clang/LLVM & WASI

Compiling C to WebAssembly using Clang/LLVM & WASI

These are my notes. See also “Compiling C to WebAssembly using clang/LLVM and WASI” by Frank Denis, which was my starting point.

Assume the following example program in a file called example.c:

#include <stdio.h>

int main(void)
{
    puts("Hello");
    return 0;
}

Linux/WSL

Download WASI SDK:

curl -LO https://github.com/CraneStation/wasi-sdk/releases/download/wasi-sdk-5/wasi-sdk-5.0-linux.tar.gz

Decompress and extract archive:

tar -xzf wasi-sdk-5.0-linux.tar.gz

Compile:

wasi-sdk-5.0/opt/wasi-sdk/bin/clang \
    --target=wasm32-wasi \
    --sysroot wasi-sdk-5.0/opt/wasi-sdk/share/sysroot \
    -Os -s -o example.wasm \
    example.c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.