【发布时间】: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。希望对你有用。