Call the C ++ library from Node.js (Node addons / node -ffi)

I am trying to integrate an external C ++ library (I have access to the .so file as well as the header files) in my Node.js. application

After much research, my options come down to:

From the definition of node -ffi gitHub I cannot say whether it will work or not work directly in C ++ libraries:

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

So, I have the following questions:

  • Does option 1) involve rewriting in some way an external C ++ library?
  • Is node-fffi the ability to directly call into C ++ libraries without any C shell I need to write?

I am not an expert when it comes to C / C ++, so if I missed something basic so that you can answer, let me know so that I can improve my question.

+5
source share
4 answers

node -ffi is apparently mainly for C programs. I went through this last week and found much better luck with node addons. You need to write a pad between the C ++ code in the library and node.js. In my case, I needed to encode and decode packets for the security protocol, so I created node buffers containing packets, and wrote C ++ code that received data from the buffers, and then sent the data to my C code, which is encoded and decoded packages.

This page: http://luismreis.imtqy.com/node-bindings-guide/docs/returning.html contains some great examples of how to get data from node.js buffers in C ++.

+4
source

What is missing in another answer? I am happy to help. Sample code is written in C ++. I illustrate how people (who make libraries in C or C ++) define an external interface for others to consume. The point of ffi is that you write your shell in any language you use (in this case javascript), and not in C / C ++ (as is the case with node extensions). If your source library is a common DLL used in other things, it already has an interface, you just need to write cover code in javascript to tell node how it works, and not write something in C ++ and expose it in the native nodejs library.

+2
source

nbind now makes it easy to write Node.js addons using C ++ external libraries. You basically create a new source file, including library headers, nbind headers, and some macro calls that list library classes and methods. Then nbind processes the rest.

libui-node is a real world example using nbind to invoke libui to create user interfaces with native widgets from Node.js. There's also a short tutorial on how to create bindings for vg , a C ++ library related to bioinformatics.

+2
source

There is a fairly simple way to link any of your libraries ( .so .dll .a ). You must add the library with the correct path to the binging.gyp file:

 { "targets": [ { "target_name": "addon", "sources": [ "hello.cc" ], "libraries": [ "path/toYourLibrary/yourLibName.dll or yourLibName.so" ] } ] } 

There is also an easier way to write good add-ons with nan . Check out the link for more info github link

0
source

All Articles