主要的工具包
- node-addon-api
- node-gyp
主要的配置
{
"variables": {
# 'module_mac': "./../sdk/mac",
},
"targets": [
{
"target_name": "native_module",
"defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"],
"cflags!": ["-fno-exceptions"],
"cflags_cc!": ["-fno-exceptions"],
"conditions": [
[
"OS==\"win\"", {
"msvs_settings": {
"VCCLCompilerTool": {
"ExceptionHandling": 1,
"RuntimeLibrary": "MultiThreadedDLL",
"AdditionalOptions": [
"/std:c++17",
"/EHsc"
]
}
},
"include_dirs": [
"node_modules/node-addon-api",
"./src/**/Include"
],
"link_settings": {
"libraries": [
"./../src/**/**/**.lib"
]
},
"defines": [
"NAPI_DISABLE_CPP_EXCEPTIONS"
],
"cflags!": ["-fno-exceptions"],
"cflags_cc!": ["-fno-exceptions"]
}
],
[
"OS==\"mac\"", {
"include_dirs": [
"node_modules/node-addon-api"
],
"mac_framework_dirs": [
"./../src/**/Lib"
],
"link_settings": {
"libraries": [
"-L./../src/**/Lib",
"**.a"
]
},
"xcode_settings": {
"DEBUG_INFORMATION_FORMAT": "dwarf-with-dsym",
"OTHER_CPLUSPLUSFLAGS": [
"-ObjC++",
"-std=c++11",
"-stdlib=libc++",
"-fvisibility=hidden",
"-frtti"
],
"OTHER_LDFLAGS": [
"-stdlib=libc++",
"-lresolv"
],
"DEPLOYMENT_POSTPROCESSING": "YES"
},
"defines": [
"NAPI_DISABLE_CPP_EXCEPTIONS"
],
"cflags!": ["-fno-exceptions"],
"cflags_cc!": ["-fno-exceptions"]
}
]
],
"sources": [
"src/native_module.cpp"
]
}
]
}
DLL 文件的运行时路径
为了确保 DLL 文件在运行时被找到,你需要将 DLL 文件放在下列路径之一:
- 可执行文件所在的目录。
- 在系统 PATH 环境变量中添加包含 DLL 文件的路径。
- 将 DLL 文件复制到项目的根目录中。
编译和运行
在项目根目录下运行以下命令:
node-gyp configure node-gyp build
编译完成后,在 Node.js 中加载和运行你的本地模块:
const yourModule = require('./build/Release/native_module');
这样,应该会正确编译并链接到 DLL 文件,并且在运行时能够找到并使用该 DLL 文件。如果还有其他问题,请提供更多详细信息以便进一步排查。