将 c-library 功能公开给节点
Expose c-library functionality to node
我一直致力于在我的节点项目中使用 c 库。经过一点调查,我找到了 node-gyp。
我能够成功执行示例,但是当我尝试在代码中使用第三方 C 库函数时,它在 运行 时给我 linking 错误。
可以在此处找到库 http://bibutils.refbase.org/bibutils_3.40_src.tgz
我独立编译库有*.a对象
我正在使用以下示例
https://github.com/nodejs/node-addon-examples/tree/master/5_function_factory/node_0.12
所以我可以推断出以下问题
- 我可以将 bibutils 从 make 转换为 gyp 吗?
- 我应该转换每个源文件以使用 V8 吗?我不知道该怎么做。
- 我怎样才能轻松地 link 这个项目以更少的噪音与 node-gyp 一起工作?
与脚本相关的详细信息可以在下面找到。 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'
执行时
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*)
问题出在 C++ 和 C 之间的通信。在上述情况下,C 头文件包含在 C++ 代码中。编译需要 C++ 代码。因此,由于编译代码不匹配,编译链接器被阻塞了。
所以我使用 extern "C" 指令通过以下代码告诉编译器有关 C 头文件的信息。
extern "C" {
#include "bibutils.h"
#include "bibprogs.h"
}
我一直致力于在我的节点项目中使用 c 库。经过一点调查,我找到了 node-gyp。
我能够成功执行示例,但是当我尝试在代码中使用第三方 C 库函数时,它在 运行 时给我 linking 错误。
可以在此处找到库 http://bibutils.refbase.org/bibutils_3.40_src.tgz
我独立编译库有*.a对象
我正在使用以下示例 https://github.com/nodejs/node-addon-examples/tree/master/5_function_factory/node_0.12
所以我可以推断出以下问题
- 我可以将 bibutils 从 make 转换为 gyp 吗?
- 我应该转换每个源文件以使用 V8 吗?我不知道该怎么做。
- 我怎样才能轻松地 link 这个项目以更少的噪音与 node-gyp 一起工作?
与脚本相关的详细信息可以在下面找到。 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'
执行时
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*)
问题出在 C++ 和 C 之间的通信。在上述情况下,C 头文件包含在 C++ 代码中。编译需要 C++ 代码。因此,由于编译代码不匹配,编译链接器被阻塞了。
所以我使用 extern "C" 指令通过以下代码告诉编译器有关 C 头文件的信息。
extern "C" {
#include "bibutils.h"
#include "bibprogs.h"
}