Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
188 lines (153 sloc) 5 KB

Tutorial Example of node binding to the vg library on MAC OS 10.11 using g++.

In order for this example to work, your project structure should look like the following:

  • Prearrangements:

Create JS_VG.cpp and fill it with whatever functionality and bindings you require. In the following example I am binding an empty constructor and two functions.

#include "../vg/src/vg.hpp"
#include "nbind/api.h"

#include "nbind/nbind.h"

namespace vg {


  // Add an empty constructor

  // Check if the graph is empty

  // Generate hash out of graph


Then we have to add relevant scripts to our `package.json', which should look like the following:

  "scripts": {
    "autogypi": "autogypi",
    "node-gyp": "node-gyp"
  "dependencies": {
    "autogypi": "^0.2.2",
    "nbind": "^0.3.5",
    "node-gyp": "^3.4.0"

Although, verify that the program versions do match the ones on your machine. I left emscripten out here, because at it's current state it is not able to handle multithreading well enough in order to be applicable to vg. Now install nbind, autogypi and node-gyp locally:

npm install --save nbind autogypi node-gyp

As nbind was designed to work with clang on MAC OS the following adjustments to the nbind module have to be made:

  1. Create g++ under ..git/project/node-modules/nbind/bin/:

# This script calls g++ (the homebrew C++ compiler on MAC OS) with given command line
# arguments. It removes the "-stdlib=libc++" flag (and its parameter), which node-gyp
# will add on OS X hosts causing a compiler error.


for arg; do
    [ "$skip" = "1" ] && skip=0 && continue
    case "$arg" in
        -stdlib=*) ;;
        *) set -- "$@" "$arg" ;;

/usr/local/bin/g++ $@

Don't forget to make it executable via:

chmod +x ..git/project/node-modules/nbind/bin/g++
  1. Open ..git/project/node-modules/nbind/src/nbind.gypi. Here comment out "-stdlib=libc++" under OTHER_CPLUSPLUSFLAGS and the whole line "OTHER_LDFLAGS": ["-stdlib=libc++"].

Now we are ready for:

npm run -- autogypi --init-gyp -p nbind -s JS_VG.cpp

Among other files a binding.gyp is created. Note that we have to point our C++ executable to our ../node_modules/nbind/bin/g++. This is reflected under make_global_settings. Furthermore, we have to add all the vg related compilation parameters in the file, finally looking like this:

"make_global_settings": [
  ["CXX",  "../node_modules/nbind/bin/g++"],
  ["LINK", "../node_modules/nbind/bin/g++"]
	"targets": [
			"includes": [
			"sources": [
			"conditions": [
				["OS=='mac'", {
					"xcode_settings": {
						"OTHER_LDFLAGS": [
	"includes": [

Feel free to add parameters for any other OS under conditions. We are ready for the compilation step now :)

npm run -- node-gyp configure build

If everything went fine, then create vg.js and fill it with:

var nbind = require('nbind');
var lib = nbind.init().lib;

var emptyGraph = new lib.VG();


Then run it with node:

node vg.js

And it should print out a hash value and true, because the graph is indeed empty. For your convenience you can find JS_VG.cpp, vg.js, binding.gyp and package.json, g++ and nbind.gypi in the following ZIP:

This tutorial is by subwaystation, under Creative Commons Attribution 4.0 International License

You can’t perform that action at this time.