Skip to content

This is a boilerplate example of how to write c++ code and translate it using emscripten into javascript. Then using a typescript declaration (definition) file or *.d.ts, how to use that in a typescript application

License

snowcoders/boilerplate-emscripten-typescript

Repository files navigation

Introduction

This is example boilerplate code of how to write code in C++ that is compiled using make and mingw32-make and then how to transpile it into javascript. Finally, it provides two example test projects, one for the javascript output and one for the C++ output.

  • client-shared-cpp - The C++ library code
  • client-shared-cpp-test - The C++ test code for the library
  • client-shared-js - The transpiled javascript code associated with the C++ library
  • client-shared-js-test-jest - A jest based javascript test suite for the transpiled library
  • client-shared-js-test-jest - A mocha based javascript test suite for the transpiled library
  • client-web-raw - A sample web application that uses basic web technologies to load the transpiled library
  • client-web-webpack - A webpack sample application that uses webpack for two websites; one in React and one in Knockout

Getting this example

git clone --recursive https://github.com/snowcoders/boilerplate-emscripten-typescript/

or if you already cloned it just run

git submodule update --init --recursive

in the root directory

Dependencies

There are several dependencies to compiling this project.

Emscripten

Emscripten is a C++ to JavaScript compiler which converts C++ code into JavaScript. http://kripken.github.io/emscripten-site/ You'll need the SDK at minimum

Windows only - Mingw64

Mingw64 allows for make to be run on windows. http://mingw-w64.org/

Setting up the CMD

Note: I've been doing this on Windows but it should compile on unix or linux with a few minor fixes. Most likely, it's the npm scripts that would give you the biggest hurdles.

Building

client-shared-cpp, client-shared-js, client-shared-cpp-test

On Windows

  1. Open a prompt window
  2. Setup the emscripten sdk by running emsdk_env.bat from wherever you installed
  3. Set the path to include your MinGW location (e.g. PATH = %PATH%;D:/MinGW/mingw64/bin)
  4. Run mingw32-make

On Unix (or Unix based system)

  1. Open a terminal window
  2. Setup the emscripten sdk by running source ./emsdk_env.sh from wherever you installed
  3. Run make

Other notes:

  • You can also run make from the root directory, and it will recurse into all the sub directories.
  • Use the -jN feature to run multiple processors for faster builds

client-shared-js-test, client-web-raw, client-web-webpack

  1. Build client-shared-js
  2. Run npm install

Running

client-shared-cpp-test

After building there should be two files

  • client-shared-cpp-test/debug/client-shared-test.exe
  • client-shared-cpp-test/ship/client-shared-test.exe

You can change the reporter status to get more details from the tests. --reporter=spec is a pretty good start, more information can be found via banditcpp's documentation

client-shared-js-test-*

  1. Go into the directory
  2. Run npm run test.

client-web-*

  1. Go into the directory
  2. Run npm run start.

Last checked versions

This example code was last verified on Windows on October 1st, 2018

Tool Version
Windows 10 Version 1803
EMCC 1.38.12 (commit 0d8576c0e8f5ee09a36120b9d44184b5da2f2e7a)
Node 8.9.1
mingw32-make v5.0.0

This example code was last verified on OSX on October 1st, 2018

Tool Version
macOS Version 10.13.6
EMCC 1.38.12 (commit 0d8576c0e8f5ee09a36120b9d44184b5da2f2e7a)
Node 8.12.0
make GNU Make 3.81

Recent updates

October 2018

  • Removed karma test example
  • Added jest test example
  • Renamed several folders for consistency
  • Updated to the latest Emscripten

About

This is a boilerplate example of how to write c++ code and translate it using emscripten into javascript. Then using a typescript declaration (definition) file or *.d.ts, how to use that in a typescript application

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published