How c++ add-on path works?


#1

Hi all,

I’m newbie to both Node and Electron.
I had two libraries, "TestLib.dylib" and "libqcocoa.dylib", then I created a "addon.node" followed nodejs/nan. It works fine at my develop folder.

Situation a) But I got this Error: dlopen(/Users/OuO/FirstApp-darwin-x64/FirstApp.app/Contents/Resources/app/build/Release/hello.node, 1): Library not loaded: libqcocoa.dylib. when I executed it as an app.

I guess that is something wrong with my binding.gyp path setting. Here is my develop folder:

  • MyAppFolder
    • cpp_plugin
    • node_modules
    • www
      • index.html
      • etc…
    • main.js
    • TestLib.dylib
    • libqcocoa.dylib
    • package.json
    • build
      • Release
        • addon.node

When I command "electron main.js" at folder MyAppFolder, everything works correctly.
Situaion b) If I type "electron MyAppFolder/main.js" at the parent folder of MyAppFolder, I would have a similar error: 1): Library not loaded: libqcocoa.dylib.

It seems like the dylib links work incorrectly. I found that electron tried to load "TestLib.dylib" and "libqcocoa.dylib " from where I typed electron. The situation b) can be fixed by adding these two library to the parent folder of MyAppFolder. However, I have no idea how to fix the situation a).

Can any one help? Appreciated.


#2

Hi there,
I finally figure my problem out. My addon.node is used in electron. The situation is, addon.node works fine when I executing in terminal. After I built it as an application, my addon.node can’t link to the dynamic libraries when I click the application icon.

The default dynamic library path of addon.node is something like"TestLib.dylib". I change it with command-line tool "install_name_tool -change" into a relative path. When I click the application icon, the runtime path is somehow out of my application bundle instead of staying with electron executable file. I added "process.chdir(__dirname);" in my main.js to back to my working directory. Finally, it links to dynamic library perfectly. Apologize for I get my problem completely wrong.

Hope this would be helpful for someone.