【问题标题】:node: symbol lookup error: , undefined symbol: _ZN4demo3sumEii节点:符号查找错误:,未定义符号:_ZN4demo3sumEii
【发布时间】:2018-02-13 17:35:47
【问题描述】:

我正在创建一个 C++ 插件并且我想使用一个静态库。我有一个 .a 库 libarith,它可以进行简单的添加。

-libarith.a
-hello.cc
-hello.js

我的binding.gyp文件如下:-

{ "targets": [
        {
        "target_name": "addon",
        "sources": [ "hello.cc" ],
        "libraries": [ "-/home/folder/api/addon/libarith.a" ],
        "cflags!": [ "-fno-exceptions" ],
        "cflags": [ "-std=c++11" ],
        "cflags_cc!": [ "-fno-exceptions" ]
        }
        ]
    }

当我编译我的 hello.cc 时,它编译得很好。但是当我运行我的插件时,它会给出以下错误:

节点:符号查找错误:/home/folder/api/addon/build/Release/addon.node: undefined symbol: _ZN4demo3sumEii.

我是插件新手,非常感谢您的帮助。

代码片段:- libarith.a 包含:

int sum(int a ,int b){

    return a+b;
} 

//你好.cc

#include <node.h>
#include <vector>
#include <iostream>
#include <v8.h>
using namespace std;
using namespace v8;
namespace demo {

using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
void newmethod(const FunctionCallbackInfo<Value>& args)
{ extern int sum(int,int);

Isolate* isolate = args.GetIsolate();
double abc= sum(4,5);
Local<Number> newnumber =Number::New(isolate,abc);
v8::String::Utf8Value r(args[1]);
    std::string rst(*r);
Local<String> first = String::NewFromUtf8(isolate, "firstargument");
Local<String> second = String::NewFromUtf8(isolate, "secondargument");
Local<Object> newobj= Object::New(isolate);
newobj->Set(first,String::NewFromUtf8(isolate, *s));
newobj->Set(second,newnumber);
args.GetReturnValue().Set(newobj);

}
void init(Local<Object> exports) {
  NODE_SET_METHOD(exports, "newmethod", newmethod);
}
NODE_MODULE(addon, init)
} 

//hello.js

const addon = require('./build/Release/addon');

var ss = "helloo";
var samplestring = "It is not a sample string";
console.log(addon.newmethod(samplestring, ss));

编辑:- 解决方案的工作原理如下。我尝试为这些库创建一个单独的目录,并且效果很好。

【问题讨论】:

  • libarith.a 包含: int sum(int a ,int b){ return a+b; } // hello.cc #include #include #include #include using namespace std;使用命名空间 v8;命名空间演示 { 使用 v8::FunctionCallbackInfo;使用 v8:: 隔离;使用 v8::Local;使用 v8::Object;使用 v8::String;使用 v8::Value; void newmethod(const FunctionCallbackInfo& args) { extern int sum(int,int);隔离* 隔离 = args.GetIsolate();双 abc= sum(4,5); Local newnumber =Number::New(isolate,abc);
  • 我附上了代码sn-p。希望对你有用。

标签: c++ node.js


【解决方案1】:

在 binding.gyp 文件中指定用于适当 .h 文件的 .so 库可以帮助我解决相同的问题:

"libraries": [
  "/usr/lib/mylib.so"
]

此链接帮助我找到它:Getting "Symbol Lookup Error" when calling C library from C++ (Node.js Addon)

【讨论】:

    【解决方案2】:

    它说,它找不到声明(.h 的实现)。我认为你给图书馆的方向是错误的。有两种解决方案:

    1. 将完整目录写入库中binding.gyp~/whereItIsLocated,在我的情况下是~/CLionProjects/node-player-core/PlayerCore/lib/x64/yourLibraryName.a
    2. 如果之前的解决方案无法解决问题,您可以将您的库复制到/usr/lib。您可以使用sudo cp ~/whereLibraryLocated /usr/lib 来完成。

    【讨论】:

    • 我都尝试了,但都没有工作,我使用命令创建了库:ar cr libarith.a sum.o .
    • 我都试过了,但都不起作用。我使用以下方法创建了库:ar cr libarith.a sum.o 我做错了什么吗?
    • 能否请您创建共享库 (.so) 并尝试这些方法?因为,我也有类似的问题。
    • 我想,你没有为函数sum()创建声明
    • 以上是我的代码,你能告诉我在哪里指定声明吗?我在 hello.cc 中声明为:extern int sum(int,int);
    猜你喜欢
    • 2012-06-19
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 2017-05-04
    • 1970-01-01
    相关资源
    最近更新 更多