我正在编写一个依赖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
我不确定这是否是您的问题,但是说服加载程序在本地目录中加载特定的库可能有些棘手。我将此部分添加到binding.gyp的targets
数组中。
技巧是告诉链接程序寻找相对于$ORIGIN
(插件所在的位置)的库。因为该插件位于build/Release
中,所以$ORIGIN
为build/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下的目录中。)