question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to load a dynamic library that needs another library?

See original GitHub issue

Hi,

I am new to Node FFI and more generally to native addons. I need to wrap a dynamic library on MacOS (let’s call it libA), which needs to load another dynamic library (libB). This is my project structure:

  • build/
    • libA.dylib
    • libB.dylib
  • lib/
    • a.js

lib/a.js is where I create my wrapper:

// [...]
const addon = ffi.Library("./build/libA.dylib", {
	// [...]
});
// [...]

Now when I want to test this, from the project root folder: node ./lib/a.js. FFI seems to be able to load libA, but then libB is not loaded:

Error: Dynamic Linking Error: dlopen(./build/libA.dylib, 2): Library not loaded: @rpath/libB.dylib
  Referenced from: /Users/jroy/Workspace/test-node-ffi/build/libA.dylib
  Reason: image not found
    at new DynamicLibrary (/Users/jroy/Workspace/test-node-ffi/node_modules/ffi/lib/dynamic_library.js:74:11)
    at Object.Library (/Users/jroy/Workspace/test-node-ffi/node_modules/ffi/lib/library.js:45:12)
    at Object.<anonymous> (/Users/jroy/Workspace/test-node-ffi/lib/a.js:31:19)
    at Module._compile (internal/modules/cjs/loader.js:721:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    at Module.require (internal/modules/cjs/loader.js:657:17)
    at require (internal/modules/cjs/helpers.js:22:18)

How am I supposed to handle this? Do I need to copy libB.dylib somewhere else (I already tried to copy it in lib and in the project root folder without success)?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

1reaction
manuxdjentcommented, Jul 15, 2019

In my case the solution was fixing the DLL path using ‘path.join’.

const dllFunctions = ffi.Library(path.join(static__, myDll.dll), { // some code });

1reaction
manuxdjentcommented, Feb 20, 2019

I have the same problem. I tried to put the dependencies in my System32 folder and it doesn’t work every time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using a shared library in another shared library - Stack Overflow
Simply use the library like you'd use it in any other application. You don't have to link ...
Read more >
4. Dynamically Loaded (DL) Libraries
Dynamically loaded (DL) libraries are libraries that are loaded at times other than during the startup of a program. They're particularly useful for ......
Read more >
Shared Libraries: Understanding Dynamic Loading
In this post, I will attempt to explain the inner workings of how dynamic loading of shared libraries works in Linux systems.
Read more >
Building And Using Static And Shared "C" Libraries
Loading A Shared Library Using dlopen(); Calling Functions Dynamically Using dlsym() ... that need to copy the library to another directory for some...
Read more >
Loading Dynamic Libraries on Mac
On Windows, when an executable needs a dynamic library, it searches for it in a few predefined locations, such as “the same directory...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found