Jump to content

Wasm: Emscripten vs WASI-SDK: Difference between revisions

From Haidhi Wiki
No edit summary
No edit summary
Line 42: Line 42:
* cara official: <code>curl https://wasmtime.dev/install.sh -sSf | bash</code>
* cara official: <code>curl https://wasmtime.dev/install.sh -sSf | bash</code>
* The Arch Linux Way: <code>sudo pacman -S wasmtime</code>
* The Arch Linux Way: <code>sudo pacman -S wasmtime</code>
Cara pakai<br>
Masih menggunakan code yang sama
<syntaxhighlight lang=cpp>
// hello.cpp
#include <stdio.h>
int main() {
    printf("Hello World\n");
    return 0;
}
</syntaxhighlight>
Lalu setelah itu:
<syntaxhighlight lang=bash>
export WASI_SDK_PATH=/opt/wasi-sdk
$WASI_SDK_PATH/bin/wasm32-wasi-clang++ \
  --sysroot=$WASI_SDK_PATH/share/wasi-sysroot \
  hello.cpp \
  -o hello.wasm
wasmtime hello.wasm
</syntaxhighlight>

Revision as of 11:43, 26 November 2025

WASI-SDK dan Emscripten adalah toolchain untuk compile C/C++ menjadi WebAssembly, tetapi targetnya berbeda:

  • WASI-SDK untuk environment WASI (non-web)
  • Emscripten untuk browser (web platform).

Emscripten

Cara install
*note: saya pakai Arch Linux

sudo pacman -S emscripten
export PATH="/usr/lib/emscripten/":$PATH

Sekarang buat code C++ simple

// hello.cpp

#include <stdio.h>

int main() {
    printf("Hello World\n");
    return 0;
}

lalu compile dengan perintah emcc

emcc hello.cpp -o hello.html

Maka secara otomatis emcc akan menghasilkan tiga file yaitu:

  • hello.html
  • hello.js
  • hello.wasm

WASI-SDK

ada beberapa cara install WASI-SDK:

saya pakai cara yang ketiga

Setalah itu install wasmtime, ini adalah runtime untuk menjalankan file .wasm
Ada dua cara:


Cara pakai
Masih menggunakan code yang sama

// hello.cpp

#include <stdio.h>

int main() {
    printf("Hello World\n");
    return 0;
}

Lalu setelah itu:

export WASI_SDK_PATH=/opt/wasi-sdk

$WASI_SDK_PATH/bin/wasm32-wasi-clang++ \
  --sysroot=$WASI_SDK_PATH/share/wasi-sysroot \
  hello.cpp \
  -o hello.wasm

wasmtime hello.wasm