NodeJS to C Binding

To call methods written in C/C++ from NodeJS you need to accomplish several tasks as stated here. I am not aware of a solution to talk directly to a C library, so I will create a C++ binding which behaves like a wrapper for the C functions. I will summarize here  how this works, please see the Github repo and the provided links for more details:

The module

To create your own node module you can create a C++ file which includes <node.h>. That file needs to declare an init function as follows:

// Initialize is your init function
void Initialize(Local<Object> exports);
// module_name will identify your module later
NODE_MODULE(module_name, Initialize)

In your init function you need to export the functions:

NODE_SET_METHOD(exports, "methodNameForJS", Method);

Here, “methodNameForJS” is the function you can call from JS; Method is the C++ function name. Method will look something like this and does some param checking and processes the call:

void Method(const FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = args.GetIsolate();

   // do something with the params here..

   // returning a string here..
   args.GetReturnValue().Set(String::NewFromUtf8(isolate, "some string"));

Building the module

To build this file on your platform you need to first declare a binding.gyp file in your project root directory:

  "targets": [
      "target_name": "module_name",
      "sources": [ "" ]
      "libraries": [ "lib_name.a" ]

I recommend you to specify the path to the .cc file in the sources field. For example, my BeagleBone did not find the .cc files when I just added their name to the sources field and placed the file in a sub directory. Also, the library is optional, of course you do not need to specify one if you don’t need any.
If you have multiple .cc files which belong to your module just add them to the sources:
"sources": [ "", "" ]

Afterwards use node-gyp:

npm install -g node-gyp
node-gyp configure
node-gyp build

This will create a “build” directory and within that a “Release” directory. That’s where the node binary will be added to. To use it just require it in your JavaScript files:

const new_module = require('./build/Release/module_name');

Neat trick: add “gypfile”:true to your package.json so that your modules are built automatically when you do a npm install.

"name": "server",
"version": "0.0.0",
"gypfile": true,

Calling C functions from C++

To call C functions declared in a C header file simply include the header file in your C++ code as such:

extern "C" {
    #include "path/to/c_header.h"

You can then call all the C functions as you used to do in plain C code. Don’t forget to include the .c files in the binding.gyp file! If you are using a C library you also need to add it to the libraries field.

Calling C++ functions from C

This is not very different from before. Just declare the function in your C++ file as follows:

extern "C" void myCppFunction(char*, void* const);

Now you can call myCppFunction(…) from your C code. Note that this only works for non-member functions.

Please refer to this guide for more information as this is only a short summary.

Footnote: I used the V8 API directly. See this guide for how to use the Native Abstraction for NodeJs (nan)

NodeJS to C Binding

One thought on “NodeJS to C Binding

Comments are closed.