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.
“court” ?
Wait, left my programmer brain at the cleaners, but I’m thinking that should be c:out ???
It’s “cout” for “character output”, the most basic mechanism to send a character to the output stream.
Right, I was hung between C and javascript, like when I leave my human language brain at the cleaners Ich weiss nicht parlez le lingo correcto.
You’re Canadian, it is understandable that you can get confused with languages!
B^)
Also “Learning Technologies” isn’t correct, it’s *leaning*
I’m leaning to it meaning “count”
B^)
WebAssembly is very interesting. Interesting project I am going to follow. Still want to port some C stuff eventually.
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.
What a mess. Hopefully it’s just headers so a clean room BSD rewrite of those files would be sufficient?
https://emscripten.org/docs/introducing_emscripten/emscripten_license.html
It’s nice to have choices. And I mean that non-snarkily. Some people like the enforced-open clause of the GPL and others don’t. It’s super that you can pick your codebase based on which particular way you want to go. What a marvelous world.
This. Diversity is necessary for evolution to take place.
opal for Ruby are better
std::cout please.
Emscripten is free and compiles C++ to JS or WebAsm.
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?
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.
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.
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.
How does Cheerp, an open-source C++ compiler for WebAssembly and JavaScript, compare to Emscripten in terms of performance, memory handling, and JavaScript interoperability?