Search code examples
c++bindingstatic-librariesnode-gyp

Node-gyp/C++ import shared library (.so)


Importing a shared library (.so) doesn't seem to be an easy task. I tried to follow the instructions in this post, but I really can't get it to work. Building without the library RF24 is working. Followed their build instructions which produced the following filesets in the /usr/local/lib folder

librf24-bcm.so        librf24.so        librf24.so.1     librf24.so.1.3  
librf24.so.1.3.1      node_modules      python2.7        python3.5

In my .cpp file I include the library like this

#include <RF24.h> // also tested "" instead of <>

My binding.gyp looks as like this

{
  "targets": [
    {
      "includes":     [ "../auto.gypi"               ],
      "sources":      [ "../../src/myfile.cpp"       ],
      "include_dirs": [ "../../src"                  ],
      "library_dirs": [ "/usr/local/lib"             ],
      "libraries":    [ "-llibrf24"                  ],
      "cflags!":      [ "-fno-exceptions"            ],
      "cflags":       [ "-std=c++11", "-fpermissive" ],
      "cflags_cc!":   [ "-fno-rtti"                  ] 
    }
  ],
  "includes": [
    "../auto-top.gypi"
  ]
}

The compiling error is

myfile.cpp:2:18: fatal error: RF24.h: No such file or directory

I also tried using the files complete names in the libraries section, e.g. librf24.so.1.3.1, and without the -l flag. Also switched the library_dirs entry to include_dirs. Still the same error.


Solution

  • According to this article I was able to figure it out. The RF24 build also has written the according header files to /user/local/include/RF24.

    Updated the binding.gyp to

    "include_dirs": [
      "../../src",
      "/usr/local/include/RF24"
    ],
    "libraries": [
      "/usr/local/lib/librf24.so.1.3.1"
    ]
    

    At least the addon is compiling without errors and warnings.