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

2019-01-15 02:29发布

问题:

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

After a lot of research my options are reduced to:

  1. Writing a Node addon

  2. Use node-ffi

From node-ffi's gitHub's definition I can't tell if it will or will not work directly on C++ libraries:

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.

So the questions I have are:

  • Does option 1) imply rewriting in some way the external C++ library?
  • Is node-ffi able to call directly to C++ libraries without any kind of C wrapper I'd have to write?

I'm no expert when it comes to C/C++ so if I missed something basic for you to be able to answer please let me know so I can improve my question.

回答1:

node-ffi seems to be primarily for C programs. I went through this in the last week, and found much better luck with node addons. What you have to do is write a shim between the C++ code in the library and node.js. In my case, I needed to encode and decode packets for a security protocol, so I made node buffers that contained the packets, and wrote C++ code that got the data out of the buffers, then send the data to my C code that encoded and decoded packets.

This page: http://luismreis.github.io/node-bindings-guide/docs/returning.html has some great examples of how to get data in and out of node.js buffers in C++.



回答2:

nbind now makes it easier to write Node.js addons using external C++ libraries. You basically create a new source file including the library headers, the nbind headers and some macro calls listing the library's classes and methods. Then nbind handles the rest.

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



回答3:

What is missing from the other answer? I am happy to help. The code example there is written in C++. I am illustrating 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 wrapper in whatever language you are using (in this case javascript) rather than C/C++ (as in the case of node extensions.) If your original library is a shared DLL used in other things, it already has an interface, you just need to write wrapper-code in javascript to tell node how it works, rather than write something in C++ and expose it in a native nodejs library.



回答4:

There is pretty easy way to link any your library(.so .dll .a). You should add library with correct path in binging.gyp file:

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

Also there is more simpler way to write good addons using nan. Check link for more information github link