[英]"The specified module could not be found" when using a custom nodejs addon
我正在編寫一個依賴於 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' ],
}
]
}
我不確定這是你的問題,但說服加載器加載本地目錄中的特定庫可能有點棘手。 我將此部分添加到 binding.gyp 中的targets
數組中。
訣竅是告訴鏈接器查找相對於$ORIGIN
(插件所在的位置)的庫。 因為插件在build/Release
中,所以$ORIGIN
是build/Release
和../../
讓你回到模塊根目錄。
通過反復試驗找到通過binding.gyp
和鏈接器的引用規則指定$ORIGIN
的正確方法。 \$$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下的一個目錄下。)
我設法讓它與這個binding.gyp
文件一起工作:
{
"targets": [
{
"target_name": "engine",
"sources": [
"addon/addon.cc"
],
"libraries": [
"legacy_stdio_definitions.lib",
"msvcrt.lib",
"msvcmrt.lib",
"<(module_root_dir)/addon/lib/glfw/opengl32.lib",
"<(module_root_dir)/addon/lib/glfw/glfw3.lib"
],
"include_dirs": [
"addon/lib",
'<!@(node -p "require(\'node-addon-api\').include")',
],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ]
}
]
}
以防萬一其他人遇到同樣的問題並最終來到這里。 插件所需的庫需要在運行時可以訪問,即使鏈接器設法找到它也是如此。
例如,如果這是在 Windows 上,並且您在構建/鏈接期間與 foo.lib 鏈接,在運行時,foo.dll 應該在同一文件夾中(對我來說它在與 .node 相同的文件夾中)或在路徑中的文件夾。 否則它不會被加載並且會拋出這個錯誤。 我認為這是非常無法解釋的錯誤。
此外,將庫保存在與 the.node 相同的文件夾中有助於隔離不同的架構構建和依賴項(x86、x64 等)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.