无法构建节点 C++ 插件

Fail to build node c++ addon

我正在从事一个使用 stereolab 的深度相机的项目 ZED to retrieve distance of obstacles. Its SDK is written in C++ and it requires CUDA 6.5. However, I have to integrate this program with another part of project, which is written in nodejs. So I decided to compile ZED code as a node module. I've gone through node addon tutorial。现在我已经编写了界面,但是在尝试使用命令 "node-gyp configure build" 构建项目后出现以下错误。 似乎没有构建 'zed' 模块。 我不知道这个错误是什么,到目前为止我什么也没搜索。有人可以给我一些指示吗?谢谢。

gyp info it worked if it ends with ok
gyp info using node-gyp@2.0.2
gyp info using node@0.12.7 | linux | x64
gyp info spawn python2
gyp info spawn args [ '/usr/lib/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'make',
gyp info spawn args   '-I',
gyp info spawn args   '/Path/to/My/Project/DepthViewer/node_zed_module/build/config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/usr/lib/node_modules/node-gyp/addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/home/joe/.node-gyp/0.12.7/common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=/home/joe/.node-gyp/0.12.7',
gyp info spawn args   '-Dnode_gyp_dir=/usr/lib/node_modules/node-gyp',
gyp info spawn args   '-Dmodule_root_dir=/Path/to/My/Project/DepthViewer/node_zed_module',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'build',
gyp info spawn args   '-Goutput_dir=.' ]
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
make: Entering directory `/Path/to/My/Project/DepthViewer/node_zed_module/build'
  CXX(target) Release/obj.target/zed/../src/main.o
/bin/sh: 1: -DNODE_GYP_MODULE_NAME=zed: not found
make: [Release/obj.target/zed/../src/main.o] Error 127 (ignored)
  SOLINK_MODULE(target) Release/obj.target/zed.node
/bin/sh: 1: -shared: not found
make: [Release/obj.target/zed.node] Error 127 (ignored)
  COPY Release/zed.node
cp: cannot stat ‘Release/obj.target/zed.node’: No such file or directory
make: *** [Release/zed.node] Error 1
make: Leaving directory `/Path/to/My/Project/DepthViewer/node_zed_module/build'
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/lib/node_modules/node-gyp/lib/build.js:269:23)
gyp ERR! stack     at ChildProcess.emit (events.js:110:17)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (child_process.js:1074:12)
gyp ERR! System Linux 3.13.0-58-generic
gyp ERR! command "node" "/usr/bin/node-gyp" "configure" "build"
gyp ERR! cwd /Path/to/My/Project/DepthViewer/node_zed_module
gyp ERR! node -v v0.12.7
gyp ERR! node-gyp -v v2.0.2
gyp ERR! not ok 

这是我的 binding.gyp:

{
  "targets": [
    {
      "target_name": "zed",
      "sources": [
        "../src/main.cpp",
      ],
      "include_dirs": [
        "/usr/local/zed/include/",
        "/usr/local/cuda-6.5/include/",
        "../include/"
      ],
    }
  ]
}

这是我的 package.json:

{
  "name": "zed_depth_viewer",
  "version": "0.0.0",
  "description": "Measure depth with zed sdk",
  "main": "zed_depth.js",
  "private": true,
  "gypfile": true,
  "dependencies": {
    "bindings": "~1.2.1"
  }
}

下面基本上是我与节点的界面:(刚开始测试,'initProgram' 正在作为项目的 'main')

void node_main_handle(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = Isolate::GetCurrent();
  HandleScope scope(isolate);

  Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, initProgram);
  Local<Function> fn = tpl->GetFunction();

  // omit this to make it anonymous
  fn->SetName(String::NewFromUtf8(isolate, "initProgram"));

  args.GetReturnValue().Set(fn);
}

void Init(Handle<Object> exports, Handle<Object> module)
{
    NODE_SET_METHOD(module, "exports", node_main_handle);
}

NODE_MODULE(zed, Init) 

使用此模块的示例应该是:

var zed = require('bindings')('zed'); //zed.node will be in './build/Release/'
var func = zed();
func();

不确定这是否有效,还没有测试过。我是 nodejs 的新手。

/bin/sh: 1: -DNODE_GYP_MODULE_NAME=zed: not found 行看似乎找不到编译器(应该是 /usr/bin/g++ 而不是 /bin/sh)。如果你想将自定义标志传递给 gyp 或者你想要一个特定的编译器,你的 gyp 文件应该像这样:

{
  'make_global_settings': [
     ['CXX','/usr/bin/clang++-3.5'],
     ['LINK','/usr/bin/clang++-3.5'],
  ],
  "targets": [
    {
      "target_name": "zed",
      "sources": [
        "../src/main.cpp",
      ],
      "include_dirs": [
        "/usr/local/zed/include/",
        "/usr/local/cuda-6.5/include/",
        "../include/"
      ],
      "link_settings": {
        "libraries": [ // For compiler
          "-requiredlibs",
        ],
        "ldflags": [  // For linker
            "-L</path/to/libs",
            "-Wl,-rpath,path/to/libs",
        ]
      },
      "cflags": [
        "-std=c++11"
      ],
    }
  ]
}

上面的 gyp 文件使用 clang(而不是 g++)进行编译和链接,并为创建的共享库设置正确的 rpath,因此您不需要导出每次要使用绑定时的库路径!

更多信息this article可能会有用!