自从我做了一些 C++ 开发以来已经有几年了。我正在尝试静态链接 boost 正则表达式库,运行时出现以下错误
节点插件.js
(我正在使用 node-addon-examples/2_function_arguments 示例)
node addon.js
dyld: lazy symbol binding failed: Symbol not found: __ZN5boost16re_detail_10600012perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE14construct_initERKNS_11basic_regexIcSD_EENS_15regex_constants12_match_flagsE
Referenced from: /Users/sjoseph/code/nodejs/addons/node-addon-examples/2_function_arguments/boost/build/Release/addon.node
Expected in: dynamic lookup
dyld: Symbol not found: __ZN5boost16re_detail_10600012perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE14construct_initERKNS_11basic_regexIcSD_EENS_15regex_constants12_match_flagsE
Referenced from: /Users/sjoseph/code/nodejs/addons/node-addon-examples/2_function_arguments/boost/build/Release/addon.node
Expected in: dynamic lookup
我在 macOS 上
nodejs: v4.3.0
g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.3.0
Thread model: posix
绑定.gyp:
{
"targets": [
{
"target_name": "addon",
"sources": [ "addon.cc" ],
"include_dirs":[
"/usr/local/Cellar/boost/1.60.0/include"
],
"link_settings":{
"libraries":[
"/usr/local/Cellar/boost/1.60.0/lib/libboost_regex.a"
],
}
}
]
}
插件.cc
#include <string>
#include <iostream>
#include <node.h>
#include <boost/regex.hpp>
using std::string;
using namespace std;
using namespace v8;
void Add(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
if (args.Length() < 2) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}
if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong arguments")));
return;
}
double value = args[0]->NumberValue() + args[1]->NumberValue();
Local<Number> num = Number::New(isolate, value);
args.GetReturnValue().Set(num);
std::string s = "Boost Libraries";
boost::regex expr{"(\\w+)\\s(\\w+)"};
boost::smatch what;
if (boost::regex_search(s, what, expr))
{
std::cout << what[0] << '\n';
std::cout << what[1] << "_" << what[2] << '\n';
}
}
void Init(Handle<Object> exports) {
NODE_SET_METHOD(exports, "add", Add);
}
NODE_MODULE(addon, Init)
插件.js
var addon = require('bindings')('addon.node')
console.log('This should be eight:', addon.add(3, 5))
注意:动态链接库也会导致问题。
问题:我需要使用相同的编译器编译 nodejs 和 boost 吗?