我一直在努力在我的节点项目中使用c库。经过一点调查,我找到了节点-杰普。
我成功地执行了示例,但是当我试图在代码中使用第三方c库函数时,它在运行时给了我链接错误。
库可以在这里找到src.tgz
我独立地编译了这个库,使其具有*.a对象
我正在使用下面的示例0.12
所以我可以推断出以下几个问题
与脚本相关的详细信息可以在下面找到。bibutils文件夹与addon.cc一起放置。
binding.gyp看起来像
{
"targets": [
{
"target_name": "addon",
"sources": [ "addon.cc" ],
"include_dirs": ["bibutils/lib"],
"library_dirs": ["bibutils/lib/libbibutil.a","bibutils/lib/libbibprogs.a"]
}
]
}改性addon.cc
#include <node.h>
#include "bibutils.h"
#include "bibprogs.h"
using namespace v8;
void MyFunction(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
/****This is not production code just to check the execution***/
bibl b;
bibl_init( &b );
bibl_free( &b );
/**************************************************************/
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "hello world"));
}
void CreateFunction(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, MyFunction);
Local<Function> fn = tpl->GetFunction();
// omit this to make it anonymous
fn->SetName(String::NewFromUtf8(isolate, "theFunction"));
args.GetReturnValue().Set(fn);
}编译结果
user1@ubuntu:~/node-addon-examples/5_function_factory/node_0.12$ npm install
> function_factory@0.0.0 install /home/user1/node-addon-examples/5_function_factory/node_0.12
> node-gyp rebuild
make: Entering directory `/home/user1/node-addon-examples/5_function_factory/node_0.12/build'
CXX(target) Release/obj.target/addon/addon.o
SOLINK_MODULE(target) Release/obj.target/addon.node
COPY Release/addon.node
make: Leaving directory `/home/user1/node-addon-examples/5_function_factory/node_0.12/build'On Execution
user1@ubuntu:~/node-addon-examples/5_function_factory/node_0.12$ node addon.js
node: symbol lookup error: /home/user1/node-addon-examples/5_function_factory/node_0.12/build/Release/addon.node: undefined symbol: _Z9bibl_initP4bibl调试信息:
user1@ubuntu:~/node-addon-examples/5_function_factory/node_0.12$ nm -C build/Release/addon.node | grep bibl_init
U bibl_init(bibl*)发布于 2015-08-04 14:34:14
问题是C++和C之间的通信,在上述情况下,C++代码中包含了一个C头文件。编译需要C++代码。因此,在编译链接器时,由于编译代码不匹配而被阻塞。
因此,我使用extern "C“指令告诉编译器关于C头文件的如下代码。
extern "C" {
#include "bibutils.h"
#include "bibprogs.h"
}https://stackoverflow.com/questions/31727546
复制相似问题