首页 > 解决方案 > 使用 nbind 神秘警告构建 C++ 扩展

问题描述

我正在使用该nbind库,并且正在使用 c++ 创建一个将在电子应用程序中使用的 promice。

到目前为止,我遵循文档说明并创建了文件./src/cpp/promice.h


#ifndef PROMICE
#define PROMICE

class MyPromice {
    public:
        MyPromice(){};
        ~MyPromice(){};
        void exec();
};
#endif

我的./src/cpp/promice.cpp

#include"promice.h"

#include <iostream>
#include<thread>
#include <chrono>

void MyPromice::exec(){
    std::this_thread::sleep_for(std::chrono::milliseconds(200));
    std::cout << "Executinh Code" << std::endl;
}

#include "nbind/nbind.h"

NBIND_CLASS(MyPromice) {
  method(exec);
}

package.json的是:

{
  "name": "electron-cpp-bindings",
  "version": "1.0.0",
  "description": "A simple test using C++ bindings on an electron app",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "electron ./src/",
    "autogypi": "autogypi",
    "node-gyp": "HOME=~/.electron-gyp node-gyp --target=v5.0.6 --arch=x64 --dist-url=https://electronjs.org/headers",
    "emcc-path": "emcc-path",
    "copyasm": "copyasm",
    "ndts": "ndts",
    "electron-version": "electron -v",
    "rebuild": "HOME=~/.electron-gyp node-gyp rebuild --target=v5.0.6 --arch=x64 --dist-url=https://electronjs.org/headers"
  },
  "author": "Dimitrios Desyllas",
  "license": "MIT",
  "devDependencies": {
    "electron": "^5.0.6",
    "electron-rebuild": "^1.8.5"
  },
  "dependencies": {
    "autogypi": "^0.2.2",
    "nbind": "^0.3.15",
    "node-gyp": "^5.0.2"
  }
}

./src/index.js的是:

const { app, BrowserWindow, ipcMain } = require('electron');

const env = process.env.NODE_ENV || 'production';
const nbind = require('nbind');
const lib = nbind.init().lib;
const MyPromice=lib.MyPromice;

// See https://stackoverflow.com/a/33067955, by Stijn de Witt
function moduleAvailable (name) {
  try {
      require.resolve (name);
      return true;
  } catch (e) {
      // empty
  }

  return false;
}

// Query for your particular module
if (moduleAvailable ("electron-debug")) require ("electron-debug") ({showDevTools:false});


// Generic on development configuration
if (env === 'dev' || env === 'debug') {
  process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
}

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
  app.quit();
}

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;

const createWindow = () => {
  // Create the browser window.
  mainWindow = new BrowserWindow();
  mainWindow.setMenu(null);
  mainWindow.maximize();

  // and load the index.html of the app.
  mainWindow.loadURL(`file://${__dirname}/ui/index.html`);
  mainWindow.on('closed', () => {
    mainWindow = null;
  });

  MyPromice.exec();

};

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);

app.on('before-quit', () => {
  if (xmpp) {
    xmpp.disconnect();
  }
});

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow();
  }
});

process.on('unhandledRejection', (reason, p) => {
  console.error('Possibly Unhandled Rejection at: Promise ', p, ' reason: ', reason);
});

process.on('SIGINT', () => {
  if (xmpp) {
    xmpp.disconnect();
  }
  process.exit(0);
});

但是由于某种原因,当我构建时:

npm run -- node-gyp   configure build 2> ~/error.txt

我得到了如下所示的错误流由于输出长度导致浏览器崩溃,我使用了 gist)。另外,命令:

npm run rebuild

返回以下输出以及在此处看到的

据我了解,这似乎是某种链接错误。你知道我该如何解决吗?

标签: c++node.jselectron

解决方案


推荐阅读