The web is about to get a lot faster by using WebAssembly (wasm). It’s main focus is to convert the code written in C/C++ into webassembly byte code.
This page is a quick guide to install all the packages related to WebAssembly, compile and run a simple Hello World! C program into WebAssembly and to execute that code in browser.
Following are the steps to install necessary tools on Ubuntu Linux distribution.
Execute following commands in Terminal for
For Python 2.7.x follow instructions here.
For compiling Emscripten from Source
Execute the following command to enable all Emscripten compiler environmental variables. If new terminal is opened, this command needs to be re-executed.
Compiling Hello World! C program into WebAssembly
Create a directory called
Save following C program into
Compile above code using Emscripten compiler. To run this program in html page, specify ouput file name with
.html extension (Here it is
hello.html). This html page runs our program in browser.
This example is tested on Firefox Nightly. To download firefox nightly:
To enable wasm on these browsers, for Chrome: open
chrome://flags/#enable-webassembly in url field and click on enable. For firefox: open
about:config in url field, search for wasm and double click on
true. After enabling wasm support
Restart the browser.
This web page,
hello.html can’t be opened directly using browser. Instead a running server is needed to view
hello.html in action. Run following command to run HTTP server.
Now open the web page in your browser by entering following line in url field.
You should be seeing
Hello World! on the output console.