我读过这个:https ://github.com/TooTallNate/node-gyp/wiki/Linking-to-OpenSSL ,但由于某种原因它对我不起作用。尝试从节点请求插件时,我收到“未定义的符号:SHA1”。这是我的代码(src/sha_export.cc
):
#include <node.h>
#include <node_buffer.h>
#include <v8.h>
#include <openssl/sha.h>
using namespace v8;
Handle<Value> Sha1(const Arguments& args) {
HandleScope scope;
if (args.Length() < 1) {
ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
return scope.Close(Undefined());
}
unsigned char*msg = (unsigned char*) node::Buffer::Data(args[0]->ToObject());
size_t msglen = node::Buffer::Length(args[0]->ToObject());
unsigned char dgst[20];
SHA1(msg, msglen, dgst);
return scope.Close(node::Buffer::New((const char*)dgst, 20)->handle_);
}
void init(Handle<Object> exports) {
exports->Set(String::NewSymbol("sha1"),
FunctionTemplate::New(Sha1)->GetFunction());
}
NODE_MODULE(token, init)
这是binding.gyp:
{
'targets': [
{
"target_name": "token"
, "sources": [ "src/sha_export.cc" ]
,'conditions': [
['node_shared_openssl=="false"', {
# so when "node_shared_openssl" is "false", then OpenSSL has been
# bundled into the node executable. So we need to include the same
# header files that were used when building node.
'include_dirs': [
'<(node_root_dir)/deps/openssl/openssl/include'
],
"conditions" : [
["target_arch=='ia32'", {
"include_dirs": [ "<(node_root_dir)/deps/openssl/config/piii" ]
}],
["target_arch=='x64'", {
"include_dirs": [ "<(node_root_dir)/deps/openssl/config/k8" ]
}],
["target_arch=='arm'", {
"include_dirs": [ "<(node_root_dir)/deps/openssl/config/arm" ]
}]
]
}]
]
}
]
}
我检查了我在 config.gypi 中是否将 node_shared_openssl 设置为 false,甚至在 /deps/openssl 中的 sha.h 中添加了 #error 以确保它被包含在内。但是,在需要插件时,我仍然会收到“未定义的符号:SHA1”,这显然意味着链接到捆绑的 OpenSSL 不起作用。如果我添加
, 'link_settings': {
'libraries': [
'-lcrypto'
]
}
之后sources
,一切正常,但随后ldd token.node
显示libcrypto.so.1.0.0 => /lib/i386-linux-gnu/libcrypto.so.1.0.0 (0xb7525000)
这意味着我现在正在链接到共享的动态 OpenSSL。所以我的问题是:是否有可能链接到与节点静态捆绑的 OpenSSL?那我做错了什么?
非常感谢!
PS 以防万一:操作系统是 Ubuntu 12.04 LTS