c - 将 c-library 功能公开给 Node

标签 c node.js node-gyp

我一直致力于在我的 Node 项目中使用 c 库。经过一点调查,我找到了 node-gyp。

我能够成功执行示例,但是当我尝试在代码中使用第三方 C 库函数时,它在运行时出现链接错误。

库可以在这里找到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 吗?我不知道该怎么做。
  • 我怎样才能轻松地将这个项目与 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 - 将 c-library 功能公开给 Node ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31727546/

相关文章:

c++ - 寻找半 float 或四分之一 float 库

c - 信号量的3个线程和消费者生产者问题

c - 指针不指向

c - 如何在 C 中将 char[] 转换为 char*?

c++ - 构建时的 node-gyp 链接库依赖项

macos - 尝试配置或重建 node-gyp 时,出现错误 : mac osx mavericks

javascript - 使用 HTML5、JS 和 Node js 进行多人绘图

node.js - Mongoose,更有效的数组搜索方式?

javascript - Passport JS : Is it possible to not serialize a user when connecting an account?

node.js - Nodejs嵌套 native 模块安装