How can I use a C++ library from node.js?
This question is related to
javascript
c++
node.js
binding
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:
- Automation - call your C++ as a standalone app in a child process.
- Shared library - pack your C++ routines in a shared library (dll) and call those routines from Node.js directly.
- Node.js Addon - compile your C++ code as a native Node.js module/addon.
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.
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.
You could use emscripten to compile C++ code into js.
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++.
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
Source: Stackoverflow.com