Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to resolve Error: dlopen("") : Library not loaded Referenced () in nodejs #2109

Closed
shivam201312 opened this issue Apr 30, 2020 · 7 comments

Comments

@shivam201312
Copy link

Hi.. i am trying to build node module using NAPI . i am using node-gyp for creating .node file
on MacOS i ran into issue

Error:


    dlopen(path-to-.node-file, 1): Library not loaded: libraryname.so Referenced from: path-to-.node-file.node

When i am running example in node_module directory then it is not throwing any error .code is running fine inside node_module.

ProjectDir/node_module/modulename/example.js -> code is running

ProjectDir/example.js -> it is showing me above error .

Structure of my module
moduefile.cc
lib.so
lib.h
binding.gyp file

binding.gyp for MacOS

['OS=="mac"',{
      'targets':[
        {
          "target_name": "target name",
          "sources":["./modulefile.cc"],
          "libraries":["<(module_root_dir)/lib.so"],
          "ldflags": ["-Wl,-rpath,'$$ORIGIN'"],
          "cflags_cc": ["-fexceptions","-fPIC","-Wno-unknown-pragmas"]
        }
      ]
    }]

So , i tried to resolve above issue made some changes in binding.gyp


    ['OS=="mac"',{
      'targets':[
        {
          "target_name": "target name",
          "sources":["./modulefile.cc"],
          "copies":[{
          "destination":"<(module_root_dir)/build/Release",
          "files":["<(module_root_dir)/lib.so"]
          }],
          "include_dirs":["<(module_root_dir)/"],
          "link_settings": {
            "libraries": [
              "-Wl,-rpath,<(module_root_dir)/lib.so"
            ],
          },
          "ldflags": ["-Wl,-rpath,'$$ORIGIN'"],
          "cflags_cc": ["-fexceptions","-fPIC","-Wno-unknown-pragmas"]
        }
      ]
    }]

It resolves above issue but throws new error while calling function of module.now i am getting error in both inside node_moduel and project root

ProjectDir/node_module/modulename/example.js -> Throwing Error

ProjectDir/example.js -> Throwing Error

Error

dyld: lazy symbol binding failed: Symbol not found: _function-name-in-lib.so-file Referenced from: targetname.node Expected in: flat namespace

From above error it seems modulefile.cc file is not able to call functions from lib.so file after i made some changes in binding.gyp.

it is strange that earlier javascript was running fine inside node_module folder but throwing error while trying to running same javascript in project root directory

Question :

  1. How can i resolve above error ?
@bnoordhuis
Copy link
Member

Check man ld, particularly the section on macos's two-level namespace. Judicious use of -flat_namespace can sometimes fix it.

@shivam201312
Copy link
Author

@bnoordhuis Thank you for replying . i have tried to add -flat_namespace

"ldflags": ["-Wl,-rpath,-flat_namespace,'$$ORIGIN'"],

but didn't worked for me .
i tried to run following command to check linked library with .node file .

otool -L filename.node
Output :
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 902.1.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.100.1)

I think there should be name of lib.so file but not sure about the same .

@shivam201312
Copy link
Author

@bnoordhuis i made some changes i tried to used .dylib instead of .so file .

"link_settings": {
            "libraries": [
              "-Wl,-rpath,<(module_root_dir)/lib.dylib"
            ],
          },

now when i am running otool -L filename.dylib command . i get following output :

      lib.dylib(compatibility version 0.0.0, current version 0.0.0)
       /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 902.1.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.100.1)

Error i am getting while calling modules from project

dlopen(path-to-.node-file, 1): Library not loaded: libraryname.so Referenced from: path-to-.node-file.node

@bnoordhuis
Copy link
Member

I think you're almost there. Try -Wl,-rpath,<(module_root_dir) and link with @rpath/lib.dylib.

@cjbj
Copy link

cjbj commented May 7, 2020

Jumping into this issue without intent to hijack it: Based on the above, I added this into node-oracledb's existing binding.gyp

	"link_settings": {
            "libraries": [
	    	    "-Wl,-rpath,$$HOME/lib"
            ],
          },

and that seems (in limited testing) seems to have solved the same problem for node-oracledb. Our install instructions suggest putting the (unchangeable) Oracle Client libraries in ~/lib so maybe this change will be acceptable.

@shivam201312
Copy link
Author

@bnoordhuis and @cjbj Thank you for replying ,so issue was when i was requiring module .js file into the project root .module file working directory changes from module folder to project root directory due to this module was not able to find .dylib . so i just added process.chdir(__dirname) and it worked for me. but there is any way i can link path with lib.dylib as it is already done with /use/lib/libc++.1.dylib .

path-to-lib/lib.dylib(compatibility version 0.0.0, current version 0.0.0)
       /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 902.1.0)

@bnoordhuis
Copy link
Member

@shivam201312 I'll close this because it sounds like you've found a working solution? If not, let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants