【问题标题】:Using custom node packages in Electron在 Electron 中使用自定义节点包
【发布时间】:2017-03-10 05:03:32
【问题描述】:

我正在尝试使用我在 Electron 应用程序中编写的自定义节点包,但无法初始化生成的 DLL/Node 包。当我启动我的 Electron 应用程序时,我收到以下错误:

Uncaught Error: A dynamic link library (DLL) initialization routine failed.

被链接的 DLL 是一个用 C++ 编写的简单库,它有一个函数,它接受一个双精度值作为输入,然后简单地向它添加一个,然后返回结果。为了构建 C++ 库,我使用 SWIG (http://www.swig.org/) 和 node-gyp 以及以下命令:

swig -c++ -javascript -node ./src/mace_api.i
node-gyp clean configure build

mace_api 是我正在尝试构建的包。 mace_api.i、binding.gyp 文件和我的库的源文件定义如下:

mace_api.i

%module MaceAPI

%{
  #include "./mace_api.cpp"
%}

%include <windows.i>
%include "./mace_api.h"

binding.gyp

{
  "targets": [
    {
      "target_name": "mace-api",
      "sources": [ "./src/mace_api_wrap.cxx" ]
    }
  ]
}

mace_api.h

#ifndef MACE_API_H
#define MACE_API_H

#include <iostream>
#include <functional>
using namespace std;

class MaceAPI
{

public:
    MaceAPI();

    double addOne(double input);

};

#endif // MACE_API_H

mace_api.cpp

#include "mace_api.h"

MaceAPI::MaceAPI()
{
}

double MaceAPI::addOne(double input)
{
    double ret = input + 1.0;
    return ret;
}

SWIG 采用 C++ 源文件,基本上编写了一个包装器,在这种情况下,node-gyp 可以使用它来构建一个 Node 包。有没有人尝试在 Electron 应用程序中使用以这种方式构建的自定义 Node 模块?我是否遗漏了 SWIG 如何为我的 C++ 库生成包装器,或者 Electron 如何处理自定义节点包?我可以将库与 Node 链接,但不能与 Electron 链接。任何帮助将不胜感激。

为了完整起见,以下是我尝试在我的 Electron 应用程序中包含和使用我的包的方式:

var libMaceTest= require('mace-api/build/Release/mace-api');
var test = new libMaceTest.MaceAPI();
console.log(test.addOne(5));

【问题讨论】:

    标签: c++ node.js swig electron


    【解决方案1】:

    你检查过https://github.com/electron/electron/blob/master/docs/tutorial/using-native-node-modules.md#manually-building-for-electron

    具体来说,

    为 Electron 手动构建

    如果您是开发原生模块并想要测试它的开发人员 针对 Electron,您可能需要为 Electron 重建模块 手动。您可以直接使用 node-gyp 为 Electron 构建:

    cd /path-to-module/ HOME=~/.electron-gyp node-gyp rebuild
     --target=1.2.3 --arch=x64 --dist-url=https://atom.io/download/atom-shell 
    

    HOME=~/.electron-gyp 改变了在哪里可以找到开发头文件。 这 --target=1.2.3 是 Electron 的版本。 --dist-url=... 指定下载标头的位置。 --arch=x64 表示模块已构建 适用于 64 位系统。

    【讨论】:

    • 当我尝试这样做时,我得到了The term 'HOME=~/.electron-gyp' is not a recognized name of a cmdlet, function, script file, or operable program. 我应该提到我在 Windows 10 上运行,但在 Ubuntu 中运行时会遇到同样的问题。
    猜你喜欢
    • 2021-12-06
    • 2021-12-08
    • 1970-01-01
    • 2016-08-12
    • 2017-06-28
    • 2020-12-28
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多