使用node-gyp将Node.js模块中的Boost链接起来

时间:2014-09-05 05:31:41

标签: c++ node.js dynamic-linking node-gyp

我正在尝试创建一个node.js附加组件,它是一个简单的包装器,用于从Boost库项目中访问perl正则表达式。

我正在运行OSX 10.9.2,而且我也不是c ++开发人员,所以工具不熟悉。

我的项目如下

boost.cc

#include <node.h>
#include <v8.h>

#include <boost/regex.hpp>

using namespace v8;

Handle<Value> Match(const Arguments& args) {
  HandleScope scope;

  if (args.Length() < 2) {
    ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
    return scope.Close(Undefined());
  }

  if (!args[0]->IsString())
  {
    ThrowException(Exception::TypeError(String::New("Regex agrument 1 must be a string")));
    return scope.Close(Undefined());
  }

  if (!args[1]->IsString())
  {
    ThrowException(Exception::TypeError(String::New("Target agrument 2 must be a string")));
    return scope.Close(Undefined());
  }

  v8::String::Utf8Value param0(args[0]->ToString());
  v8::String::Utf8Value param1(args[1]->ToString());

  std::string sre = std::string(*param0);
  std::string s = std::string(*param1); 

  try
  {
    boost::cmatch matches;
    boost::regex re(sre, boost::regex::icase);

    if (boost::regex_match(s.c_str(), matches, re))
    {
      return scope.Close(Boolean::New(true));
    } 
    else
    {
      return scope.Close(Boolean::New(false));
    } 
  }
  catch (boost::regex_error& e)
  {
    ThrowException(Exception::TypeError(String::New("Regex is not a valid regular expression")));
    return scope.Close(Undefined());
  }

  return scope.Close(Boolean::New(false));
}

void init(Handle<Object> exports) {
  exports->Set(String::NewSymbol("match"),
      FunctionTemplate::New(Match)->GetFunction());
}

NODE_MODULE(boost, init)

binding.gyp

{
  "targets": [
    {
      "target_name": "boost",
      "sources": [ "boost.cc" ],
      "include_dirs": [
              "/usr/local/include/boost",
            ],
            "libraries": [
                "/usr/local/lib/libboost_regex.dylib"
            ]
        }
    ]
}

app.coffee

addon = require('../addons/boost/build/Release/boost')
console.log(addon.match("(?<!street)name", "StreetName"))

运行node-gyp rebuild --verbose会导致成功构建加载项。当我运行应用程序时,我收到以下错误:

dyld: lazy symbol binding failed: Symbol not found: __ZN5boost9re_detail12perl_matcherIPKcSaINS_9sub_matchIS3_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE14construct_initERKNS_11basic_regexIcSA_EENS_15regex_constants12_match_flagsE
  Referenced from: <projects dir>/addons/boost/build/Release/boost.node
  Expected in: dynamic lookup

dyld: Symbol not found: __ZN5boost9re_detail12perl_matcherIPKcSaINS_9sub_matchIS3_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE14construct_initERKNS_11basic_regexIcSA_EENS_15regex_constants12_match_flagsE
  Referenced from: <projects dir>/addons/boost/build/Release/boost.node
  Expected in: dynamic lookup

我已经使用binding.gyp和otool玩了几个小时,但我显然已经超出了我的深度。

输出上的otool给了我以下内容:

otool -L build/Release/boost.node
build/Release/boost.node:
    /usr/local/lib/libboost_regex.dylib (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 60.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)
    /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 2577.0.0)

我用brew安装了boost库(brew install --without-python boost),所有的动态库似乎都在那里。

我的问题与我的问题类似:node-gyp on OSX 10.7.5 -- dyld: lazy symbol binding failed: Symbol not found - 但我对此修复没有运气。

我可以在没有依赖项的情况下运行基本的附加组件,只是看起来我无法使用node-gyp等正确链接Boost库。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:7)

答案是使用c ++ 11编译节点加载项。只需要对binding.gyp进行一些调整即可。

<强> binding.gyp

{
  "targets": [
    {
      "target_name": "boost",
      "sources": [ "boost.cc" ],
      "include_dirs": [
              "/usr/local/include/boost",
            ],
            "libraries": [
                "/usr/local/lib/libboost_regex.dylib"
            ],
            "cflags_cc!": [ "-fno-rtti", "-fno-exceptions" ],
            "cflags!": [ "-fno-exceptions" ],
            "conditions": [
                [ 'OS=="mac"', {
                    "xcode_settings": {
                        'OTHER_CPLUSPLUSFLAGS' : ['-std=c++11','-stdlib=libc++', '-v'],
                        'OTHER_LDFLAGS': ['-stdlib=libc++'],
                        'MACOSX_DEPLOYMENT_TARGET': '10.7',
                        'GCC_ENABLE_CPP_EXCEPTIONS': 'YES'
                    }
                }]
            ]
        }
    ]
}