Nbind
:sparkles: Magical headers that make your C++ library accessible from JavaScript :rocket:
Install / Use
/learn @charto/NbindREADME
Quick start | Requirements | Features | User guide | Contributing | License
nbind is a set of headers that make your C++11 library accessible from JavaScript.
With a single #include statement, your C++ compiler generates the necessary bindings
without any additional tools. Your library is then usable as a Node.js addon or,
if compiled to asm.js with Emscripten,
directly in web pages without any plugins.
nbind works with the autogypi dependency management tool,
which sets up node-gyp to compile your library without needing any configuration
(other than listing your source code file names).
nbind is MIT licensed and based on templates and macros inspired by
embind.
Quick start
C++ everywhere in 5 easy steps using Node.js, nbind and autogypi:
The above is all of the required code. Just copy and paste in the mentioned files and prompts or take a shortcut:
git clone https://github.com/charto/nbind-example-minimal.git
cd nbind-example-minimal
npm install && npm test
See it run!
(Note: nbind-example-universal is a better starting point for development)
Requirements
You need:
- Node.js 0.10.x - 7.x.x (newer may also work).
- Python 2.7, NOT 3.x (required by
node-gyp, see instructions).
And one of the following C++ compilers:
- GCC 4.8 or above.
- Clang 3.6 or above.
- Emscripten 1.35.0 or above.
- Visual Studio 2015 (the Community version is fine).
Features
nbind allows you to:
- Use your C++ API from JavaScript without any extra effort.
- From Node.js, Electron and web browsers (using asm.js on Chrome, Firefox and Edge).
- On Linux, OS X and Windows.
- Without changes to your C++ code. Simply add a separate short description at the end.
- Distribute both native code and an asm.js fallback binary.
- Automatically generate TypeScript
.d.tsdefinition files from C++ code for IDE autocompletion and compile-time checks of JavaScript side code.
In more detail:
- Export multiple C++ classes, even ones not visible from other files.
- Export C++ methods simply by mentioning their names.
- Auto-detect argument and return types from C++ declarations.
- Automatically convert types and data structures between languages.
- Call C++ methods from JavaScript with type checking.
- Pass JavaScript callbacks to C++ and call them with any types.
- Pass instances of compatible classes by value between languages (through the C++ stack).
The goal is to provide a stable API for binding C++ to JavaScript. All internals related to JavaScript engines are hidden away, and a single API already supports extremely different platforms.
Works on your platform
<table> <tr> <th>Target</th> <th colspan=2>Development platform</th> </tr><tr> <th></th> <th>Linux / OS X</th> <th>Windows</th> </tr><tr> <td>Native</td> <td> <a href="http://travis-ci.org/charto/nbind"> <img src="https://travis-ci.org/charto/nbind.svg?branch=master" alt="Build status"> </a> </td> <td> <a href="https://ci.appveyor.com/project/jjrv/nbind/branch/master"> <img src="https://ci.appveyor.com/api/projects/status/xu5ooh1m3mircpde/branch/master?svg=true" alt="Build status"> </a> </td> </tr><tr> <td>Asm.js</td> <td> <a href="http://travis-ci.org/charto/nbind-ci-emscripten"> <img src="https://travis-ci.org/charto/nbind-ci-emscripten.svg?branch=master" alt="Build status"> </a> </td> <td>Tested manually</td> </tr> </table>Roadmap
More is coming! Work is ongoing to:
- Precompile to a single native library for all versions Node.js and Electron on the same platform
- Precompiled addons for different Node.js versions for efficiently calling the library will be provided with nbind
- Support native Android and iPhone apps.
Future 0.x.y versions should remain completely backwards-compatible between matching x and otherwise with minor changes.
Breaking changes will be listed in release notes of versions where y equals 0.
Contributing
Please report issues through Github and mention the platform you're targeting (Node.js, asm.js, Electron or something else). Pull requests are very welcome.
Warning: rebase is used within develop and feature branches (but not master).
When developing new features, writing tests first works best. If possible, please try to get them working on both Node.js and asm.js. Otherwise your pull request will get merged to Master only after maintainer(s) have fixed the other platform.
Installing Emscripten to develop for asm.js can be tricky. It will require
Python 2.7 and setting paths correctly, please refer to
Emscripten documentation.
The bin/emcc script in this package is just a wrapper,
the actual emcc compiler binary should be in your path.
You can rebuild the asm.js library and run tests as follows:
npm run clean-asm && npm run prepublish && npm run test-asm
User guide
- Installing the examples
- Creating your project
- Configuration
- Calling from Node.js
- Using nbind headers
- Functions
- Classes and constructors
- Inheritance <sup>new in 0.3.5</sup>
- Methods and properties
- Overloaded functions <sup>new in 0.3.2</sup>
- Getters and setters
- Passing data structures
- Callbacks
- Using objects
- Type conversion <sup>updated in 0.3.2</sup>
- Buffers <sup>new in 0.3.1</sup>
- 64-bit integers <sup>new in 0.3.0</sup>
- Error handling
- Publishing on npm
- Shipping an asm.js fallback
- Using in web browsers <sup>updated in 0.3.0</sup>
- Using with TypeScript <sup>updated in 0.3.5</sup>
- Binding plain C
- Binding external libraries
- Debugging
- Alternatives
Installing the examples
nbind examples shown in this user guide are also available to download
for easier testing as follows:
Extract this zip package or run:
git clone https://github.com/charto/nbind-examples.git
Enter the examples directory and install:
cd nbind-examples
npm install
Creating your project
Once you have all requirements installed, run:
npm init
npm install --save nbind autogypi node-gyp
nbind, autogypi and node-gyp are all needed to compile
a native Node.js addon from source when installing it.
If you only distribute an asm.js version, you can use
--save-dev instead of --save because users won't need to compile it.
Next, to run commands without installing them globally, it's practical
to add them in the scripts section of your package.json that npm init
just generated. Let's add an install script as well:
"scripts":

