【问题标题】:dyld: lazy symbol binding failed: Symbol not found. Expected in: flat namespacedyld:惰性符号绑定失败:未找到符号。预期在:平面命名空间
【发布时间】:2019-07-02 13:00:29
【问题描述】:

我正在尝试使用我同事编写的代码为节点创建自己的 c++ 插件。

它编译为file.node,然后当我尝试在节点中使用它时它崩溃了。

我尝试过预构建库,然后使用 library.dylib 并通过 node-gyp 一起构建它。

这两种方法都会编译并在运行时抛出错误。

我还能做什么?

我正在开发 OSX Mojave。

我已经检查过:

How to include c++ libraries so that node-gyp can link?

dyld: lazy symbol binding failed

整个错误:

dyld: lazy symbol binding failed: Symbol not found: 
__ZN3mds7computeERNSt3__16vectorINS1_IdNS0_9allocatorIdEEEENS2_IS4_EEEE
Referenced from: /.../node_folder/build/release/file.node
Expected in: flat namespace

我的 gyp 文件:

{
  "targets": [
     {
       "target_name": "name",
       "sources": ["file.cc"],
       "include_dirs": [
         "<!(node -e \"require('nan')\")",
         "/path/to/cpp/src/"
       ],
       "link_settings": {
         "libraries": ["-L/path/to/dylib/directory"]
       },
       "libraries": ["-L/path/to/dylib/directory"]
    }
  ]
}

我的 package.json

{
 ...
 "dependencies": {
   "nan": "^2.12.1",
   "node-gyp": "^3.8.0"
 },
 "scripts": {
   "compile": "node-gyp rebuild",
   "start": "node index.js"
 },
 "gypfile": true
}

我的绑定文件:

#include <nan.h>
#include <iostream>
#include <my_header_file.h>

using namespace v8;

NAN_METHOD(compute)
{
    if (!info[0]->IsArray())
    {
        Nan::ThrowTypeError("Argument myst be an array");
        return;
    }
    ...

    std::vector<std::vector<double>> vector;
     
    ... (filling the vector with data)

    //static std::vector<std::vector<double>> compute(std::vector<std::vector<double>> & distances_matrix);
    mds::compute(vector);
}

NAN_MODULE_INIT(Initialize)
{
    NAN_EXPORT(target, compute);
}

NODE_MODULE(addon, Initialize);

【问题讨论】:

  • 你能解开:__ZN3mds7computeERNSt3__16vectorINS1_IdNS0_9allocatorIdEEEENS2_IS4_EEEE好吗?你显然有一个缺失的符号。知道它是什么可能会使修复变得显而易见。
  • @MartinBonner 我已经编辑了 cpp 代码 -> 我已经添加了我认为会抛出它的方法。
  • @MartinBonner 我们怎样才能对符号进行解构?有这样的工具吗?
  • demangler.com@Jonathan

标签: c++ node.js node-gyp


【解决方案1】:

我已经从https://github.com/nodejs/node-gyp/issues/1380解决了,

缺少的符号名称表明它没有使用 C 链接。

我只是在头文件中添加extern "C"

extern "C" double add(double a ,double b);

【讨论】:

    【解决方案2】:

    我看到您正在 #include &lt;my_header_file.h&gt; 导入您的标头。 如果您从您的自定义类中为您的 NAN_METHOD 调用一个方法,您需要内联调用它,否则编译器将不知道在哪里查找。

    运行 "c++filt (missing symbol)" 去解码,看看你需要在哪里调用它

    示例 而不是method() 使用Class::method()

    你的缺失符号是mds::compute(std::__1::vector&lt;std::__1::vector&lt;double, std::__1::allocator&lt;double&gt; &gt;, std::__1::allocator&lt;std::__1::vector&lt;double, std::__1::allocator&lt;double&gt; &gt; &gt; &gt;&amp;)

    【讨论】:

    • 这解决了我的问题。只是为了让其他用户更清楚:在您的班级主体中,Napi::Value method(...) {} 应该变为 Napi::Value ClassName::method(...) {}
    猜你喜欢
    • 2013-04-30
    • 2011-03-19
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多