Wasm Builders 🧱

Paul Nwoko
Paul Nwoko

Posted on • Updated on

Setting up Development Environment for generating WebAssembly file from C/C++ source.

In this tutorial I will show you how to setup the development environment for compiling C programs to WASM and executing the WASM module using wasmtime runtime.

Download Clang, a C/C++ Compiler.

Upstream Clang and LLVM (from 9.0 onwards) can compile for WASI out of the box, and WebAssembly support is included in them by default.

Download and install clang :

sudo apt install clang
Enter fullscreen mode Exit fullscreen mode

when installation is complete, run this command to verify your installation.

clang --version
Enter fullscreen mode Exit fullscreen mode

Result:

clang version 10.0.0-4ubuntu1 
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Enter fullscreen mode Exit fullscreen mode

Next, download the WASI-sdk.The wasi-sdk provides a clang which is configured to target WASI and use the WASI sysroot by default if you put the extracted tree into /, so we can compile our program like:

opt/wasi-sdk/share/sysroot/bin/clang demo.c -o demo.wasm
Enter fullscreen mode Exit fullscreen mode

If you would want to extract it elsewhere, you can specify the sysroot directory like this:

~/wasi-sdk-12.0/bin/clang demo.c --sysroot <path to sysroot> -o demo.wasm
Enter fullscreen mode Exit fullscreen mode

If you're using the wasi-sdk, the sysroot directory is located in opt/wasi-sdk/share/sysroot/ on Linux and Mac.

This is just regular clang, configured to use a WebAssembly target and sysroot. The output name specified with the "-o" flag can be anything you want, and does not need to contain the .wasm extension. In fact, the output of clang here is a standard WebAssembly module:

Install Runtime environment:

You could use either wasmtime or wasmer or other runtime environments

Install WASMTIME

The WASMTIME is a wasm runtime environment for running WebAssembly modules. The easiest way to install the wasmtime CLI tool is through this installation script. Linux and macOS users can execute the following:

curl https://wasmtime.dev/install.sh -sSf | bash
Enter fullscreen mode Exit fullscreen mode

This will download a precompiled version of wasmtime

You can confirm your installation works by executing:

wasmtime -V
Enter fullscreen mode Exit fullscreen mode

which result in:

wasmtime 0.12.0
Enter fullscreen mode Exit fullscreen mode

To run a wasm with wasmtime

wasmtime <filename>.wasm
Enter fullscreen mode Exit fullscreen mode

OR Install WASMER

WASMER is an open-source runtime for executing WebAssembly on the Server. To install it run the following command on your terminal

curl https://get.wasmer.io -sSfL | sh
Enter fullscreen mode Exit fullscreen mode

To verify your installation:

wasmer --version
Enter fullscreen mode Exit fullscreen mode

result:

wasmer 2.0.0
Enter fullscreen mode Exit fullscreen mode

To run a wasm with wasmer

wasmer run <filename>.wasm
Enter fullscreen mode Exit fullscreen mode




Reference:

about wasmtime

about wasmer

Top comments (0)