Skip to content
kripken edited this page Dec 17, 2011 · 40 revisions

Emscripten Tutorial

Emscripten is an open source LLVM to JavaScript compiler. With it, you can compile C and C++ code into JavaScript and run it on the web. And it's easy!

Requirements

To use Emscripten and complete this tutorial, you need a few things:

  • The Emscripten code, from github
  • LLVM with Clang (3.0)
  • Node.js (0.5.5 or above)
  • Python

After getting those if you don't already have them, change directory to where you put the Emscripten code. The rest of this tutorial will assume that that is where you are running commands (or, add a path to another location if you prefer).

Before continuing, it's a good idea to make sure the requirements work. Try

clang tests/hello_world.cpp
./a.out

That should use Clang and LLVM compile a "hello world" app and run it. Then, do

node tests/hello_world.js

which should also print out "hello world".

...

Next Steps

After completing this tutorial, you can do more sophisticated things with Emscripten. For that, it is recommended to also get

  • The Closure Compiler. Emscripten generates code that is designed to be processed correctly in Closure Compiler's advanced mode, which can both minify and optimize for speed in significant ways. The Closure Compiler is run automatically by emcc in -O2 and above.
  • The SpiderMonkey shell. Almost, but not all of the tests in Emscripten's test suite can run using Node.js. It's also a good idea to test using more than one engine anyhow.

Clone this wiki locally