How can I use a C++ library from node.js?

JavascriptC++node.jsBinding

Javascript Problem Overview


How can I use a C++ library from node.js?

Javascript Solutions


Solution 1 - Javascript

There is a fresh answer to that question now. SWIG, as of version 3.0 seems to provide javascript interface generators for Node.js, Webkit and v8.

I've been using SWIG extensively for Java and Python for a while, and once you understand how SWIG works, there is almost no effort(compared to ffi or the equivalent in the target language) needed for interfacing C++ code to the languages that SWIG supports.

As a small example, say you have a library with the header myclass.h:

#include<iostream>

class MyClass {
        int myNumber;
public:
        MyClass(int number): myNumber(number){}
        void sayHello() {
                std::cout << "Hello, my number is:" 
                << myNumber <<std::endl;
        }
};

In order to use this class in node, you simply write the following SWIG interface file (mylib.i):

%module "mylib"
%{
#include "myclass.h"
%}
%include "myclass.h"

Create the binding file binding.gyp:

{
  "targets": [
    {
      "target_name": "mylib",
      "sources": [ "mylib_wrap.cxx" ]
    }
  ]
}

Run the following commands:

swig -c++ -javascript -node mylib.i
node-gyp build

Now, running node from the same folder, you can do:

> var mylib = require("./build/Release/mylib")
> var c = new mylib.MyClass(5)
> c.sayHello()
Hello, my number is:5

Even though we needed to write 2 interface files for such a small example, note how we didn't have to mention the MyClass constructor nor the sayHello method anywhere, SWIG discovers these things, and automatically generates natural interfaces.

Solution 2 - Javascript

Look at node-ffi.

> node-ffi is a Node.js addon for loading and calling dynamic libraries using pure JavaScript. It can be used to create bindings to native libraries without writing any C++ code.

Solution 3 - Javascript

You can use a node.js extension to provide bindings for your C++ code. Here is one tutorial that covers that:

http://syskall.com/how-to-write-your-own-native-nodejs-extension

Solution 4 - Javascript

You could use emscripten to compile C++ code into js.

Solution 5 - Javascript

There newer ways to connect Node.js and C++. Please, loot at Nan.

EDIT The fastest and easiest way is nbind. If you want to write asynchronous add-on you can combine Asyncworker class from nan.

Solution 6 - Javascript

Here is an interesting article on Getting your C++ to the Web with Node.js

> three general ways of integrating C++ code with a Node.js application > - although there are lots of variations within each category: > > 1. Automation - call your C++ as a standalone app in a child process. > 2. Shared library - pack your C++ routines in a shared library (dll) and call those routines from Node.js directly. > 3. Node.js Addon - compile your C++ code as a native Node.js module/addon.

Solution 7 - Javascript

Try shelljs to call c/c++ program or shared libraries by using node program from linux/unix . node-cmd an option in windows. Both packages basically enable us to call c/c++ program similar to the way we call from terminal/command line.

Eg in ubuntu:

const shell = require('shelljs');

shell.exec("command or script name");

In windows:

const cmd = require('node-cmd');
cmd.run('command here');

Note: shelljs and node-cmd are for running os commands, not specific to c/c++.

Solution 8 - Javascript

Becareful with swig and C++: http://www.swig.org/Doc1.3/SWIG.html#SWIG_nn8

> Running SWIG on C++ source files (what would appear in a .C or .cxx file) is not recommended. Even though SWIG can parse C++ class declarations, it ignores declarations that are decoupled from their original class definition (the declarations are parsed, but a lot of warning messages may be generated). For example: > /* Not supported by SWIG */ int foo::bar(int) { ... whatever ... }

It's rarely to have a C++ class limited to only one .h file.

Also, the versions of swig supporting JavaScript is swig-3.0.1 or later.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaiaVictorView Question on Stackoverflow
Solution 1 - JavascriptenobayramView Answer on Stackoverflow
Solution 2 - JavascriptVadim BaryshevView Answer on Stackoverflow
Solution 3 - JavascriptalexgolecView Answer on Stackoverflow
Solution 4 - JavascriptN3UR0CHR0MView Answer on Stackoverflow
Solution 5 - JavascriptJasurbek NabijonovView Answer on Stackoverflow
Solution 6 - JavascriptsreepurnaView Answer on Stackoverflow
Solution 7 - JavascriptSomView Answer on Stackoverflow
Solution 8 - JavascriptAntonView Answer on Stackoverflow