Skip to content

WebAssembly C/C++ Examples

Overview

These C and C++ examples demonstrate how to create different types of WebAssembly (wasm) programs with the twr-wasm library.

These are good examples to use as starting points for your own Wasm projects.

These examples are a good place to learn how to configure clang and wasm-ld to compile and link C/C++ code for use with WebAssembly (wasm).

Hello World

Name Description Link
helloworld A very simple C Wasm example to get you started helloworld

Console Examples

Name Description Link
divcon A simple C program demos inputting and printing characters to a div tag divcon
terminal A simple C program demos writing and inputting from a <canvas> tag
that twr-wasm configures as a windowed "terminal"
terminal
multi-io Demo 6 simultaneous consoles: stream i/o, terminal, and 2D Drawing. multi-io

Draw 2D and Audio Examples

Name Description Link
balls These fun Bouncing Balls are written in C++ and demo the 2D drawing
APIs with a C++ Canvas wrapper class
balls
pong A simple game of Pong written in C++ to demo 2D drawing and Audio APIs with
a C++ canvas wrapper class and taking user input from JS
pong
maze This is an old Win32 program ported to wasm and demos 2D Draw APIs maze

Call Argument Examples

Name Description Link
callC A demo of passing and returning values between JavaScript and Wasm module callc
fft A demo of calling a C library to perform an FFT that is graphed in TypeScript fft

twrLibrary Examples

Name Description Link
lib A demo of createing a twrLibrary (use TypeScript to create C/C++ APIs) library

Unit Tests

Name Description Link
tests twr-wasm unit tests tests
tests-user "cli" for tests using libc++ and <canvas> tests-user
tests-libcxx Smoke test for libc++. Shows how to use libc++. tests-libcxx
tests-d2d Unit tests for Draw 2D canvas console tests-d2d
tests-audio Unit tests for the Audio Library tests-audio

Running or Building the examples locally

Online versions of the examples can be viewed here.

You can also run the examples locally, or build them..

Copying Examples to Start your own Project

All of the examples have makefiles that use a relative path for twr.a and includes. These paths will work fine if your code is in an examples sub-folder as a peer to the other examples. But assuming your code is in your own project folder elsewhere, you will need to determine the correct path to twr.a and includes for your project's makefile. Details on how to do this can be found in the following sections: Hello World walk through and the Compiler and Linker Options section.

Also see the section on Import Resolution if you installed with git clone.