【问题标题】:Error: Cannot open shared object file: No such file or directory错误:无法打开共享对象文件:没有这样的文件或目录
【发布时间】:2018-01-03 08:12:21
【问题描述】:

我正在为 Node.js 编写 C++ 插件。尝试使用名为 libSample.so 的示例库,其中声明了函数 printHello:

void printHello() {
    std::cout << "Hello World\n";
}

效果很好。(使用node-gyp configure build编译并执行node ./)

当我尝试使用另一个更复杂的库 libCore.so 时。开始执行时产生以下错误。编译配置通过find:

module.js:597
  return process.dlopen(module, path._makeLong(filename));
                 ^

Error: libPlayerCore.so: cannot open shared object file: No such file or directory
    at Error (native)
    at Object.Module._extensions..node (module.js:597:18)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/jasurn/CLionProjects/JsTest/hello.js:2:15)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)

libCore.so 的一些用法

    //#include <core.h> definition of core library lies in this header
    void CreateObject(const FunctionCallbackInfo<Value>& args) {
        Isolate* isolate = args.GetIsolate();
        Local<Object> obj = Object::New(isolate);
        obj->Set(String::NewFromUtf8(isolate, "msg"), args[0]->ToString());
        //usage of core library
        Core core;
        args.GetReturnValue().Set(obj);
    }

binding.gyp 文件:路径正确,因为它与另一个库一起使用:)

{
  "targets": [
    {
      "target_name": "addon",
      "sources": [ "hello.cc" ],
      "libraries": [
        "/home/jasurn/CLionProjects/JsTest/libPlayerCore/lib/libCore.so"
                ]
    }
  ]
}

我将不胜感激您的回答或建议!

【问题讨论】:

  • 错误很明显,节点找不到你的插件库。您可以找到有关节点如何对模块进行搜索的相关信息here。
  • @VTT 有趣的是它在同一目录中找到了另一个库。但是当我尝试使用 libCore.so 时。它显示这样的错误。谢谢你的链接!

标签: c++ node.js add-on node.js-addon


【解决方案1】:

找到了简单的解决方案,但我可能不适合长期开发。我认为问题在于您的共享库所需的依赖库。您可以在终端lld libCore.so上通过命令查看所需的库

    linux-vdso.so.1 =>  (0x00007ffcae9d6000)
    libasound.so.2 => /usr/lib/x86_64-linux-gnu/libasound.so.2 (0x00007fa19dc07000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fa19d9e9000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fa19d7e5000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fa19d45c000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fa19d153000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fa19cf3a000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa19cb73000)
    /lib64/ld-linux-x86-64.so.2 (0x00005637d3532000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fa19c96b000)

在我的共享库的依赖库列表上方。

解决方案:

您应该将共享库复制到 /usr/lib 位置。这样就解决了我的问题。

cp path/where/yourLocated/libCore.so /usr/lib 

【讨论】:

  • 您能解释一下为什么不能将这些 .so(共享对象文件)添加到 linux 系统上的 /opt/ 目录吗?我知道这个问题更多的是关于 linux 文件系统层次结构。
  • 是的,我想你可以把它放在那里,但是你应该在项目配置中链接它。我可能是错的,因为有一段时间没有做过 c++ 和 node js。也许2020年会有新的选择。无论如何祝你好运。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-12
  • 2018-11-26
  • 2019-11-27
  • 2020-02-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多