A tour of Cheerp
A 5-minute tour of Cheerp and its features
What Cheerp does
Cheerp is a C and C++ compiler for web applications. It can compile C and C++ codebases to WebAssembly and JavaScript which can run in the browser or Node.js.
Cheerp is open source, cross-platform, and available under the Apache 2.0 license.
Who Cheerp is for
Cheerp is perfect for:
- Web developers who want to use C and C++ libraries in their web applications or websites.
- C/C++ developers who want to write new web applications in C and C++.
- C/C++ developers who want to port their existing application or library to the web.
Features
Compile to WebAssembly and JavaScript with the same codebase
Cheerp can compile parts of your code into JavaScript, and other parts into WebAssembly. This is helpful because they have different memory models, and therefore they have different limitations.
By default, Cheerp code compiles to WebAssembly. This default can be changed with the -target
command-line option.
To compile a class or function to JavaScript, attach the [[cheerp::genericjs]]
attribute. This will compile it to JavaScript and place it in the genericjs section.
Use Web APIs in C++ with zero overhead
Cheerp provides a C++ namespace client
which exposes JavaScript and Web APIs. You can use these APIs to interact with the browser and JavaScript environment. This feature only works in genericjs code.
Calling client
functions is zero-overhead and have the same semantics as they have in JavaScript.
Expose C++ classes and methods to JavaScript
You can export classes and functions by marking them with the [[cheerp::jsexport]]
attribute.
This will add a function factorial
to JavaScript’s global object.
You can also combine this feature with JavaScript Modules:
Inline JavaScript code in C++
In genericjs, you can use the __asm__
keyword to write native (JavaScript) code, and pass arguments and get values back as usual.