在使用node-gyp构build时,无法将nodeJS本机C ++插件链接到与节点(0.10.18)静态绑定的OpenSSL

我读过这个: 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中将#error放在sha.h中,以确保它包含在内。 不过,在需要插件的时候,我还是会得到“未定义的符号: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)这意味着我链接到共享dynamic的OpenSSL而不是现在。 所以我的问题是:是否可以链接到与节点静态捆绑的OpenSSL? 那我做错了什么?

非常感谢你!

PS以防万一,这很重要:操作系统是Ubuntu 12.04 LTS

那么,回答我自己的问题…从本Noordhuison在node.js IRC上得到一些帮助。 非常感谢Ben!

显然,节点可执行文件暴露的OpenSSL例程有限,基本上只有那些节点使用自己,在我的情况下,不包括更高级别的SHA1函数,但是它包含了较低级别的函数:SHA1_Init, SHA1_Update和SHA1_Final。 改变我的代码看起来像

 SHA_CTX ctx; SHA1_Init(&ctx); SHA1_Update(&ctx, msg, msglen); SHA1_Final(dgst, &ctx); 

而不仅仅是SHA1(msg, msglen, dgst); 并没有外部依赖它工作正常。

据Ben介绍,在Windows上连接静态OpenSSL也可能存在一些问题:不能对此进行评论,只能使用Linux。