Webassembly on Ubuntu 18.04 Part 1

How to install toolchain

git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest

Setup Environment after Installation

You will need run this each time before your development.

source ./emsdk_env.sh --build=Release

Build and run a simple program

  • To generate a HTML page we need specify the output name with .html extension.
  • Due to cross-origin-request-sharing(CORS) is not supported by file protocol, you need run a http server for the generate html. You can use for example python3 -m http.server etc or use emrun
mkdir hello
cd hello
cat << EOF > hello.c
#include <stdio.h>
int main(int argc, char ** argv) {
  printf("Hello, world!\n");
}
EOF

emcc hello.c --emrun -o hello.html

emrun --no_browser --port 8080 .

Now open your browser with http://localhost:8080/hello.html and click on hello.html, you shall see webassembly code running.