0

I have been attempting to make a nodejs-native-addon which uses libmtp to carry out certain functions. I have been successful in the building the app but the app is throwing Library not loaded: /usr/local/lib/libmtp.9.dylib. Referenced from: /path/build/Debug/nbind.node. Reason: image not found error when I try to run it on another macbook where the libmtp isn't installed.

This is my binding.gyp file:

{
  "targets": [
    {
      "includes": [
        "auto.gypi"
      ],
      "sources": [
        "src/native/mtp.cc"
      ],
      "link_settings": {
        "libraries": [
            "-lmtp"
        ],
      },
    }
  ],
  "includes": [
    "auto-top.gypi"
  ],
}

I even attempted to include the dylib file in the libraries option

"link_settings": {
    "libraries": [
      "<(module_root_dir)/src/native/lib/libmtp.9.dylib"
    ]
}

but the app fails to start with the Library not loaded: /usr/local/lib/libmtp.9.dylib. Referenced from: /path/build/Debug/nbind.node. Reason: image not found error.

Any help will be appreciated.

Ganesh Rathinavel
  • 1,243
  • 4
  • 17
  • 38

2 Answers2

0

The error is indicating that the library libmtp.9.dylib cannot be found in the standard library include path /usr/local/lib Try setting the environment variable LD_LIBRARY_PATH to point to the location where you have the libmtp.9.dylib before running the node.

Surya
  • 41
  • 3
  • Wow that's great, let me take a look. A quick question though is it possible to bundle such libraries automatically along with the release file? What if I have another 3rd party library imported in the code and it will get difficult to keep track of the dependencies to ship along with the app. Is there anyway to automatically resolve and bundle the dependent libraries along with the app? – Ganesh Rathinavel Jan 24 '20 at 13:17
  • You can add another target section to the targets[] array of the binding.gyp file to copy the library to the `build/Release` or `build/Debug` folder. Here is a sample section: `{ "target_name": "copy_lib", "type":"none", "dependencies" : [ "your_addon_name" ], "copies": [ { 'destination': '<(module_root_dir)/build/Release/', 'files': ['<(module_root_dir)/src/native/lib/libmtp.9.dylib'] } ] }` – Surya Jan 24 '20 at 14:19
  • I did exactly the same. Tried export LD_LIBRARY_PATH=/absolute/path/library and even LD_LIBRARY_PATH=/absolute/path/library/libmtp.9.dylib before running the nodejs file. But i keep getting Library not loaded: /usr/local/lib/libmtp.9.dylib. Referenced from: /path/build/Debug/nbind.node. Reason: image not found error. Am I missing anything here? – Ganesh Rathinavel Jan 24 '20 at 18:11
0

One solution would be to create a symlink in a known rpath like /usr/local/lib manually to your built library. Not ideal but it may provide a workaround for at least having successful builds in development.

ln -s <absolute_path>/src/native/lib/libmtp.9.dylib /usr/local/lib/libmtp.9.dylib 

This allows the binding.gyp file to find the library without it needing to configure an rpath with whatever process is throwing the error. This is easier in my opinion than tracking down the binding.gyp trace.

max
  • 175
  • 1
  • 11