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

相关推荐
感哥3 小时前
C++ STL 常用算法
c++
_AaronWong3 小时前
Electron 桌面应用侧边悬浮窗口设计与实现
前端·electron
saltymilk14 小时前
C++ 模板参数推导问题小记(模板类的模板构造函数)
c++·模板元编程
感哥14 小时前
C++ lambda 匿名函数
c++
沐怡旸20 小时前
【底层机制】std::unique_ptr 解决的痛点?是什么?如何实现?怎么正确使用?
c++·面试
感哥21 小时前
C++ 内存管理
c++
博笙困了1 天前
AcWing学习——双指针算法
c++·算法
感哥1 天前
C++ 指针和引用
c++
感哥2 天前
C++ 多态
c++
沐怡旸2 天前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试