taybenlor/runno-clang - wapm
Wapm logo

The search results will appear here


0.1.2 Public • Published a month ago18.27 MB

Clang is a compiler front end for the C, C++, Objective-C and Objective-C++ programming languages

Copy and paste to install this package
wapm install taybenlor/runno-clang

launchGithub (grade - call_split - error_outline - open issues/PRs)


This package ships the full clang compiler and it's Wasm linker (wasm-ld), so we can compile C programs to WASI using just WebAssembly.

Note: This repo is extending the great work from Ben Smith published in his CppCon 2019 WebAssembly talk. The WebAssembly binaries are copied from Ben's fork of llvm-project.

Run clang!

In this example, we will pass our C program to clang via stdin. Then, we will run the Wasm linker to generate the final .wasm program.

# Run the compiler
echo 'int printf(const char *, ...); int main(){printf("hello world!\n");}' | wapm run clang -cc1 -triple wasm32-unknown-wasi -isysroot /sys -internal-isystem /sys/include -emit-obj -o ./example.o -

# Run the Wasm linker
wapm run wasm-ld -L/sys/lib/wasm32-wasi /sys/lib/wasm32-wasi/crt1.o ./example.o -lc -o ./example.wasm

And last, but not least... run it with a WebAssembly runtime!

wasmer example.wasm


When you run clang normally (eg. clang example.c -o ./example), it will spawn two different process under the hood:

  1. The compiler: clang -cc1 -triple wasm32-unknown-wasi -isysroot /sys -internal-isystem /sys/include -emit-obj -o ./example.o ./example.c
  2. The linker: wasm-ld -L/sys/lib/wasm32-wasi /sys/lib/wasm32-wasi/crt1.o ./example.o -lc -o ./example.wasm

However the posix_spawn required syscall is not available in WASI.

Because of that, we need to run this two different calls ourselves.