【发布时间】:2023-03-03 05:14:28
【问题描述】:
我正在编写一个依赖于 OpenGL (glfw) 的 nodejs 插件。它编译成功,但是当我尝试在节点中使用它时,我收到错误The specified module could not be found。
这是插件 C++ 代码的问题部分:
#include <glfw/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
glfw
glfw3.dll
glfw3.h
glfw3.lib
glfw3dll.lib
glfw3native.h
opengl32.lib
addon.cc
编辑: 新的 binding.gyp:
{
"targets": [
{
"target_name": "engine",
"sources": [
"addon/addon.cc"
],
"libraries": [
"-lglfw3dll",
"-lopengl32",
"-L<module_root_dir)/lib/glfw",
"-Wl,-rpath,\$$ORIGIN/../../lib",
],
"include_dirs": [
"addon/lib",
'<!@(node -p "require(\'node-addon-api\').include")'
],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
}
]
}
【问题讨论】:
-
你如何尝试在节点中使用它?
-
@bmacnaughton 就像一个插件模块,带有
const engine = require('bindings')('engine');。它可以在不包含任何外部库的情况下工作。 -
文件是否存在?
ls -R build在包根目录下? -
@bmacnaughton 对不起,我可能应该澄清一下。我在运行插件时没有问题。我得到了插件编译和运行。当我添加 OpenGL 时它才开始崩溃。
-
从本地位置加载库可能有点棘手。我将尝试提供一个示例作为答案-它可能会也可能不会解决您的问题。但我假设只有在您尝试加载
glfw3dll.lib时它才开始失败。
标签: c++ node.js glfw node.js-addon node-addon-api