Meet Cheerp, an alternative to Emscripten

Cheerp is a C/C++ compiler for web applications - Compiles to WebAssembly and JavaScript.


Meet Cheerp, an alternative to Emscripten


Cheerp is an open source C/C++ compiler, with permissive, enterprise-grade license for Web applications. It allows compiling practically any C/C++ code for WebAssembly and JavaScript.

It is based on and integrated with the LLVM/Clang infrastructure, and presents several stages of custom optimizations to maximize performance and minimize the size of compiled JavaScript/WebAssembly output.

Cheerp is primarily used to port existing C/C++ libraries and applications to HTML5, but can also be used to write high-performance Web applications and WebAssembly components from zero.

Cheerp


Installation

There are ready-made binaries for Windows, Ubuntu/Debian and macOS.

  • For Windows you can download the latest version from the releases page and run the file https://github.com/leaningtech/ cheerp-meta/releases/download/cheerp-3.0/Cheerp-3.0-Windows.exe;

  • For Debian and Ubuntu just add the repository and install via APT:

# Add the following line to /etc/apt/sources.list
deb http://ppa.launchpad.net/leaningtech-dev/cheerp-ppa/ubuntu focal main

# And import the key to apt with the command
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 84540D4B9BF457D5
sudo apt update

# Then just install
sudo apt install cheerp-core
  • For macos you can also download the latest version from the releases page and run the file https://github.com/leaningtech /cheerp-meta/releases/download/cheerp-3.0/cheerp-macosx-3.0.dmg;

To build from scratch just follow the instructions at this link.


Usage

A basic Hello, World! would be:

Create a basic file in C++, for example, with the following code:

#include <cheerp/clientlib.h>

void webMain(){
   client::console.log("Hello, World Wide Web!");
}

Compile with the command:

/opt/cheerp/bin/clang++ -target cheerp hello.cpp -o hello.js

And run with Node.js:

nodejs hello.js

You can also use your own index.html file:

<!DOCTYPE html>
<html lang="en">
   <head>
     <meta charset="utf-8">
     <title>Cheerp example 0: hello</title>
     <script defer src="hello.js"></script>
   </head>
   <body>
   <h1 id="pagetitle">Open the console log
(Ctrl + Shift + J or Ctrl + Option + J)
to read the output</h1>
   </body>
</html>

Remembering that some browsers won’t run on ~file:///~, but you can use python3 -m http.server and then open the file.


For more information see the links:


cpp C language web webassembly


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles