我正在使用nbind
库,并且正在用c ++创建一个供电子应用程序使用的Promice。
到目前为止,我已经按照documentation的说明进行操作,并创建了文件./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
我得到以下错误流,如here所示(由于使用过长的输出,导致浏览器崩溃,因此使用了gist)。另外,命令:
npm run rebuild
返回以下输出以及在here中看到的
据我了解,这似乎是某种链接错误。你知道我该怎么解决吗?