Home » Nodejs » node-gyp link library dependencies at build time

node-gyp link library dependencies at build time

Posted by: admin November 30, 2017 Leave a comment

Questions:

My nodejs addon uses cares library and my binding.gyp has dependencies attribute which is pointing to this. Now whenever I have to run addon, I have to export LD_LIBRARY_PATH variable.

Is there a way I can configure this in binding.gyp so that every time I don’t have to set LD_LIBRARY_PATH. (Linking at build time)

Dependency setting in binding.gyp: "dependencies": [ "deps/cares/cares.gyp:cares" ]

Ex export: export LD_LIBRARY_PATH=build/Release

Answers:

"libraries": [ "-Wl,-rpath,./build/Release/" ]

Worked for me on Mac as well as Linux(Ubuntu).

Questions:
Answers:

My answer to how to add dependence to static library in binding.gyp node-gyp for node.js extension seems to apply here as well, and should be cross platform.

In short it uses the module_root_dir and builds an absolute path.
Example:

"libraries": [
    "-lcares",
    "-L<(module_root_dir)/build/Release/"
]

Questions:
Answers:

I have had success obtaining a relative path in Ubuntu & Debian using the following binding.gyp line:

"libraries": [ "../lib/somelib/somelib.a" ]

My folder structure looks like this:

|--binding.gyp
|--app.cc
|--lib
   |--somelib
      |--somelib.a

I got the method (somewhat indirectly) from here:
https://github.com/nodejs/node-gyp/issues/49

Leave a Reply

Your email address will not be published. Required fields are marked *