Emscripten is the way!

emscripten

Hundreds and hundreds of C code. BOOM! You need them all translated to Javascript in few hours! Hard algorithms to handle and almost nothing to optimize.

Don’t panic! Emscripten is here to help us in this difficult task.

Emscripten is an LLVM-to-JavaScript compiler. It takes LLVM bitcode – which can be generated from C/C++, using llvm-gcc or clang, or any other language that can be converted into LLVM – and compiles that into JavaScript, which can be run on the web (or anywhere else JavaScript can run).

Using Emscripten, you can

  • Compile C and C++ code into JavaScript and run that on the web
  • Run code in languages like Python as well, by compiling CPython from C to JavaScript and interpreting code in that on the web

You can go through the project at the following link: https://github.com/kripken/emscripten/

Steps are, relatively, easy:
1) Download the code;
2) Unzip it and go into the folder;
3) Run the following command:

./emcc --help

At this point, a file containing the main path dependencies will be created, and Emscripten will be ready to use (crossed fingers for dependencies).

Now let’s try this command:

./emcc tests/hello_world.cpp -o my_first_example.html

The command above will generate, if successful, three files (a *.html, *html.map and a *.js where the main bitcode has been translated to Javascript).

Double click on the html file and watch the awesomeness on you browser :)
That’s it!!! I suggest to go through the generated code to understand how everything has been translated and a plus would be comparing the existing codes (Javascript vs C/C++) to understand the logic.

For more info visit the following link: https://github.com/kripken/emscripten/wiki

However, in a world as technology changes quickly in a really short period, it makes sense creating something portable to various platforms (e.g. Android,iOS, Wp, FirefoxOS, computers).

In lots of cases it is worth to choose a simple solution as this one and conquering the market with a cross platform product.

Francesco Vitullo

I'm a passionate developer!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>