napi-bind
napi-bind
is a header-only C++ library that takes advantage of two things:
- C++ can do template deduction
- C functions have relatively simple types
By combining these, napi-bind
is able to automatically wrap C functions into N-API bindings with minimal fuss, and no external dependencies or libraries.
Usage
Setup
-
Add
napi-bind
as a dev dependency to your npm or yarn project:$ npm install --save-dev napi-bind# Or$ yarn add --dev napi-bind -
Add the include directories for
napi-bind
to yourbinding.gyp
file:{'targets': [{'target_name': 'my_binding_library','include_dirs': ['<!@(node -p "require(\'napi-bind\').include")',# [...]],# [...]}# [...]]# [...]} -
Add
#include <napi_bind.hpp>
to your C++ headers and begin adding bindings!
Code
See the test directory for some examples of usage.
Limitations
Note that while this extends to simple C++ functions, napi-bind
isn't really designed to do more complex wrapping of C++ objects. You might be able to get it to do so with some cleverness, but no guarantees.
If you are looking to create more complex C++ bindings, consider one of the following two libraries: