Emscripten- Getting started

Imagine that you have written a very large project in C/C++ and now you want the same in Java Script. Will you start writing each piece of code in Java Script? Isn’t it too tedious?

Emscripten is an open source LLVM to JavaScript compiler. Using which, you can compile C and C++ code into JavaScript and run it on the web. This avoids the need to write the whole bunch of code program from the scratch.

Some of the requirements before using emscripten:[1]

  • The Emscripten code, from github (git clone git://github.com/kripken/emscripten.git)
  • LLVM with Clang (3.1 is the officially supported version)
  • Node.js (0.5.5 or above)
  • Python 2.6

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. Clang is a new C/C++/Objective-C/Objectice-C++ compiler being developed on top of LLVM. This must be a good replacement for gcc.

Here are the steps to build and install clang. [2]

  1. mkdir /path/to/clang-build
  2. cd /path/to/clang-build
  3. cd llvm/tools
  4. cd ../..
  5. mkdir build
  6. cd build
  7. ../llvm/configure –enable-optimized –disable-assertions
  8. make && sudo make install

Node.js: In order to execute the JavaScript you intend to run in the backend, it needs to be interpreted and, well, executed. This is what Node.js does. [3]

1. git clone https://github.com/joyent/node.git
2. cd node
3. ./configure
4. make
5. sudo make install

Once you have successfully done this. The next step is to setup emscripten. You can run emscripten by typing ./emcc. This ma throw some error if the path to clang and node is not correct.

Now we can compile the first helloworld file using emscripten.

./emcc tests/hello_world.cpp
  node a.out.js

Hello world! gets printed 🙂
Hope this post helped you get started with emscripten.

[1] https://github.com/kripken/emscripten/wiki/Tutorial
[2] http://ehsanakhgari.org/blog/2011-10-18/why-you-should-switch-clang-today-and-how
[3] http://samcaldwell.net/index.php/technical-articles/3-how-to-articles/120-how-do-i-install-nodejs-on-ubuntu-1004


Tagged: , ,

2 thoughts on “Emscripten- Getting started

  1. gaurav December 27, 2012 at 3:27 am Reply

    Hi- I have tried copying the binaries to usr/local on ubuntu. I’m now able to use clang. but ./emcc doesn’t work. I tried installing following ur guidelines; but the problem persists. Am I missing something. Iam trying install it on ubuntu 11.04.
    thanks in advance!

    • Sowmya Ravidas December 27, 2012 at 1:34 pm Reply

      Hi gaurav,

      Could you please explain the error that you are getting when you do ./emcc?
      Check whether your node.js and clang works independently.
      Also, did you make the changes in the settings file?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s