C++ Compiler Targets The Web

It is a common problem these days. You have a piece of code in C or C++. Maybe it is older code. Or maybe you prefer prototyping your ideas using C. But, inevitably, someone now wants your code to run in a Web browser. The options for making this happen have expanded quite a bit lately and one possibility is Cheerp, an open-source compiler that handles up to C++ 17 and can output to WebAssembly, JavaScript, or asm.js.

The compiler is free to use for GPLv2 projects. If you aren’t open yourself, it looks like you have to cut a deal to use Cheerp with its maker, Learning Technologies.

Traditionally, you’d use Emscripten to do something like this. According to the project’s site, Cheep generates faster and smaller WebAssembly than Emscripten and has several advantages if you compile to JavaScript. For example, they claim to have better dynamic memory handling, more efficient access to the DOM, and better JavaScript interoperability.

The Hello World example is, understandably, a little underwhelming, but does show some special features that allow direct access to the browser. They do point out that you can ignore that and use things like printf or cout if you aren’t worried about performance. A better place to start if you want to do anything serious is the pong game example.

If you haven’t been keeping up with WebAssembly, we can get you started. If you don’t think any of this would have application in an embedded system, we’ll refer you to Olaf.

18 thoughts on “C++ Compiler Targets The Web

  1. Big fat warning:

    > If you are using our free GPL version you should release the code. You are using GPL headers and linking with GPL
    libraries so your code is considered derived work of cheerp.

    1. What i wanted to say, Emscripten exists and works.

      The question i always ask when someone wants to hand me a product with lesser freedoms than my current to-go software offers me, what is the benefit of that tool versus my tool, what does it do better that should make me use it?

  2. The referred Olaf project only uses WASM as a second platform that is totally independent of any embedded system. It could equally well be a standalone Windows desktop program, that doesn’t use the microcontroller at all.

    Just clarifying, as I was expecting the ESP32 to create a webpage and using WASM, which it does not!

    So Olaf is not project that shows why WASM would be useful in embedded systems, though WASM is cool.

  3. The referred Olaf project only uses WASM as a second platform that is totally independent of any embedded system. It could equally well be a standalone Windows desktop program, that doesn’t use the microcontroller at all.

    Just clarifying, as I was expecting the ESP32 to create a webpage and using WASM, which it does not!

    So Olaf is not project that shows why WASM would be useful in embedded systems, though WASM is cool.

    1. While it is true that it is not part of the embedded system, it did allow him to port his embedded code to the web for demonstration and it was part of the whole project and maybe even a key piece since most of us won’t build the hardware. I can easily see using a C/C++ to web tool for prototyping, showing investors/stakeholders, or even for pushing my code into a web-enabled device.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.