Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add dependence to static library in binding.gyp node-gyp for node.js extension

I have a simple binding.gyp file for building my node.js extension. I want to change the linking method of the library "mylib" and link it statically instead of using shared library.

{
  "targets": [
    {
      "target_name": "myext",
      "sources": [
        "code/main.cpp",
        "code/load.cpp",
        "include/load.h"
      ],
      "include_dirs": [
        "include", "../Library/include"
      ],
      "libraries": [
        "-lmylib", "-L/home/admin/MyLib/Library/binaries/linux/Release"
      ],
      "cflags!": [ "-fno-exceptions" ],
      "cflags": [ "-std=c++11" ],
      "cflags_cc!": [ "-fno-exceptions" ]
    }
  ]
}

The static and shared versions of mylib are in the same directory:

# ls /home/admin/MyLib/Library/binaries/linux/Release
libmylib.a libmylib.so

I want to link statically to avoid library installing/loading issues.

like image 517
Alex Netkachov Avatar asked Sep 11 '13 18:09

Alex Netkachov


People also ask

Why is node-gyp required?

node-gyp is a tool that enables the compilation of native add-on modules for Node in multiple platforms. It has widespread use and is included as a dependency in many NPM packages. On most systems, this isn't an issue, and installing node-gyp with the rest of your packages works as expected.

What version of Python does node-gyp use?

HOWEVER, the documentation of node-gyp also states that it requires Python>=3.6 or Python<=3.9 .

Which file does node-gyp use to read the configuration?

The binding.gyp file A binding.gyp file describes the configuration to build your module, in a JSON-like format. This file gets placed in the root of your package, alongside package.json .

Does node-gyp require Python 2?

To use node-gyp, first, we'll need to install a Python runtime, the make utility, and a C or C++ compiler. Already, we run into an issue with the first requirement. node-gyp expects Python ≥v3. 6, not Python v2.


2 Answers

You could have node-gyp insert the path for you with the built in variable module_root_dir If I interpret your code correctly, changing your libraries to e.g.:

"libraries": [
    "-lmylib",
    "-L<(module_root_dir)/../Library/binaries/linux/Release"
],

might do the trick. Though I'm not sure if it will link with the .a or .so version, to specify that you could try:

"libraries": [
    "<(module_root_dir)/../Library/binaries/linux/Release/libmylib.a"
],

without the -L prefix and -lmylib. Further more, I'm a bit unsure if you're allowed to traverse above the root directory though. I have not tested using anything above the module root directory before. Other than that you should be good to go.

like image 109
Ale Avatar answered Oct 28 '22 07:10

Ale


Just add the path of static .a or .so file in the "libraries" section,

"libraries": [ "/usr/local/lib/libnetfilter_queue.so.1.3.0" ]

like image 42
Val Avatar answered Oct 28 '22 09:10

Val