electron调用c++ dll lib

主要的工具包

  1. node-addon-api
  2. 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 文件。如果还有其他问题,请提供更多详细信息以便进一步排查。

相关推荐
WBluuue23 分钟前
Codeforces 1076 Div3(ABCDEFG)
c++·算法
u01092727134 分钟前
模板编译期排序算法
开发语言·c++·算法
m0_686041611 小时前
C++中的适配器模式变体
开发语言·c++·算法
恒者走天下1 小时前
cpp c++辅导星球价格调整
c++
rainbow68892 小时前
VSCode配置C/C++环境全攻略
c++
naruto_lnq2 小时前
C++中的桥接模式
开发语言·c++·算法
j445566112 小时前
C++中的职责链模式高级应用
开发语言·c++·算法
WarmSword2 小时前
mac上用cursor/vscode调试root权限进程
c++·ide·vscode·macos·mac
m0_736919102 小时前
模板元编程性能分析
开发语言·c++·算法
wbs_scy3 小时前
C++11:类新功能、lambda与包装器实战
开发语言·c++