“使用自定义nodejs插件时找不到指定的模块”

问题描述 投票:0回答:1

我正在编写一个依赖OpenGL(glfw)的nodejs插件。它编译成功,但是当我尝试在节点中使用它时,出现错误The specified module could not be found

这是附加C ++代码的问题部分:

#include <gl/glfw3.h>

if(glfwInit()) {
    printf("glfw init success");
}
else {
    printf("glfw init failed");
}

使用此插件,它会编译,但会导致节点出错。没有它,它将编译并运行而不会出现问题。

这是我的binding.gyp:

{
  "targets": [
    {
      "target_name": "engine",
      "sources": [
        "addon/addon.cc"
      ],
      "libraries": [
            "<(module_root_dir)/addon/lib/gl/glfw3dll.lib"
        ],
      "include_dirs": [
        "addon/lib",
        "<!@(node -p \"require('node-addon-api').include\")"
      ],
      'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
    }
  ]
}

和插件文件结构:

addon
  lib
    gl
      glfw3.dll
      glfw3.h
      glfw3.lib
      glfw3dll.lib
      glfw3native.h
  addon.cc
c++ node.js glfw node.js-addon node-addon-api
1个回答
0
投票

我不确定这是否是您的问题,但是说服加载程序在本地目录中加载特定的库可能有些棘手。我将此部分添加到binding.gyp的targets数组中。

技巧是告诉链接程序寻找相对于$ORIGIN(插件所在的位置)的库。因为该插件位于build/Release中,所以$ORIGINbuild/Release,并且../../将您带回到模块根目录。

只是花了反复试验才能找到通过$ORIGIN和链接程序的引用规则指定binding.gyp的正确方法。 \$$ORIGIN导致$ORIGIN被嵌入到节点插件中。

'conditions': [
    ['OS in "linux"', {
    # includes reference glfw3dll/glfw3dll.h, so
    'include_dirs': [
      '<!@(node -p "require(\'node-addon-api\').include")',
        '<(module_root_dir)/'
    ],
    'libraries': [
        '-lglfw3dll',
        '-L<(module_root_dir)/dir-for-glfw3dll/',
        '-Wl,-rpath-link,<(module_root_dir)/dir-for-glfw3dll/',
        '-Wl,-rpath,\$$ORIGIN/../../dir-for-glfw3dll/'
    ],
    }]
]

((我将文件名更改为您的文件,并将其放在module_root_dir下的目录中。)

© www.soinside.com 2019 - 2024. All rights reserved.