node.js - 使用 node-gyp 构建时,无法将 nodeJS native C++ 插件链接到与 Node (0.10.18) 静态绑定(bind)的 OpenSSL

标签 node.js linker openssl add-on node-gyp

我读过这个:https://github.com/TooTallNate/node-gyp/wiki/Linking-to-OpenSSL ,但由于某种原因它对我不起作用。当我尝试从 Node 请求插件时,我收到“ undefined symbol :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)

这是绑定(bind).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 以确保包含它。但是,在需要插件时,我仍然收到“ undefined symbol :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。 所以我的问题是:是否可以链接到与 Node 静态捆绑的 OpenSSL?那我做错了什么?

非常感谢!

PS 以防万一,操作系统是 Ubuntu 12.04 LTS

最佳答案

好吧,回答我自己的问题...在 Node.js IRC 上从 Ben Noordhuison 获得了一些帮助。非常感谢本!

显然, Node 可执行文件公开的 OpenSSL 例程数量有限,基本上,只有 Node 使用自身的例程,在我的例子中,不包括更高级别的 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。

关于node.js - 使用 node-gyp 构建时,无法将 nodeJS native C++ 插件链接到与 Node (0.10.18) 静态绑定(bind)的 OpenSSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18908619/

相关文章:

javascript - react 连接到 Node Cors 预检失败

javascript - 将全局变量传递给 Javascript 中的事件回调函数?

c - 使用GDB时缺少ELF符号 "var"?

c - OpenSSL 错误 : unable to verify the first certificate

c++ - 简单的 C++ OpenSSL 代码崩溃

javascript - 如何在 Node 中同时运行多个命令

windows - DLL 中 Unresolved 符号错误

c++ - 通过函数指针定义 "main"

c - 在不知道底层算法的情况下如何复制 EVP_PKEY 结构?

javascript - 如何从连接回调之外的应用程序中的任何位置发出套接字?