Local use of WebAssembly
We compile a C++ source in wasm in order to locally run the target code.
1) Install Emscriptem
This program is simply downloaded from github and installed as explained in the article First WebAssembly program.
2) Write a small demonstration program in C ++
We will use the same example given in the installation tutorial.
Example:
#include <stdio.h>
int main() {
puts("Hello, World!");
}
Save the program under the name hello.cpp
3) Compile to wasm
To generate the environment after opening the console:
./emsdk
To generate the code:
emcc hello.cpp -o hello.wasm
This produces the following file:
hello.wasm
The hello.wasm file is a binary program. To view the content you can use wasm dump or wasm2asm or wasm-dis which is included in Emscriptem.
Local execution
To make the wasm code work locally, you can use Wasmer which is included in Emscriptem or installed separately.
wasmer hello.wasm
Which displays “Hello, World!”.