{"id":27558,"date":"2020-12-22T07:07:42","date_gmt":"2020-12-22T05:07:42","guid":{"rendered":"http:\/\/cornay.co.za\/wp\/?p=27558"},"modified":"2020-12-22T07:07:42","modified_gmt":"2020-12-22T05:07:42","slug":"c-compiler-targets-the-web","status":"publish","type":"post","link":"http:\/\/cornay.co.za\/wp\/2020\/12\/22\/c-compiler-targets-the-web\/","title":{"rendered":"C++ Compiler Targets the Web"},"content":{"rendered":"<p>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 <a href=\"https:\/\/github.com\/leaningtech\/cheerp-meta\" target=\"_blank\" rel=\"noopener\">Cheerp<\/a>, an open-source compiler that handles up to C++ 17 and can output to WebAssembly, JavaScript, or asm.js.<\/p>\n<p>The compiler is free to use for GPLv2 projects. If you aren&#8217;t open yourself, it looks like you have to cut a deal to use Cheerp with its maker, Learning Technologies.<\/p>\n<p><span id=\"more-453037\"><\/span><\/p>\n<p>Traditionally, you&#8217;d use Emscripten to do something like this. According to the project&#8217;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.<\/p>\n<p>The <a href=\"https:\/\/github.com\/leaningtech\/cheerp-meta\/wiki\/Getting-started#hello-world\" target=\"_blank\" rel=\"noopener\">Hello World example<\/a> 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 <code>printf<\/code> or <code>cout<\/code> if you aren&#8217;t worried about performance. A better place to start if you want to do anything serious is the <a href=\"https:\/\/github.com\/leaningtech\/cheerp-meta\/wiki\/Cheerp-Tutorial:-Mixed-mode-C++-to-WebAssembly-and-JavaScript\" target=\"_blank\" rel=\"noopener\">pong game example<\/a>.<\/p>\n<p>If you haven&#8217;t been keeping up with <a href=\"https:\/\/hackaday.com\/2019\/04\/04\/webassembly-what-is-it-and-why-should-you-care\/\">WebAssembly<\/a>, we can get you started. If you don&#8217;t think any of this would have application in an embedded system, we&#8217;ll refer you to <a href=\"https:\/\/hackaday.com\/2020\/08\/30\/olaf-lets-an-esp32-listen-to-the-music\/\">Olaf<\/a>.<\/p>\n<p>source <a href=\"https:\/\/hackaday.com\/2020\/12\/21\/c-compiler-targets-the-web\/\">https:\/\/hackaday.com\/2020\/12\/21\/c-compiler-targets-the-web\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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<span class=\"ellipsis\">&hellip;<\/span><\/p>\n<div class=\"read-more\"><a href=\"http:\/\/cornay.co.za\/wp\/2020\/12\/22\/c-compiler-targets-the-web\/\">Read more <span class=\"screen-reader-text\">C++ Compiler Targets the Web<\/span><span class=\"meta-nav\"> &#8250;<\/span><\/a><\/div>\n<p><!-- end of .read-more --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-27558","post","type-post","status-publish","format-standard","hentry","category-news-2"],"_links":{"self":[{"href":"http:\/\/cornay.co.za\/wp\/wp-json\/wp\/v2\/posts\/27558","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cornay.co.za\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cornay.co.za\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cornay.co.za\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cornay.co.za\/wp\/wp-json\/wp\/v2\/comments?post=27558"}],"version-history":[{"count":1,"href":"http:\/\/cornay.co.za\/wp\/wp-json\/wp\/v2\/posts\/27558\/revisions"}],"predecessor-version":[{"id":27559,"href":"http:\/\/cornay.co.za\/wp\/wp-json\/wp\/v2\/posts\/27558\/revisions\/27559"}],"wp:attachment":[{"href":"http:\/\/cornay.co.za\/wp\/wp-json\/wp\/v2\/media?parent=27558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cornay.co.za\/wp\/wp-json\/wp\/v2\/categories?post=27558"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cornay.co.za\/wp\/wp-json\/wp\/v2\/tags?post=27558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}