博客
关于我
Electron框架下使用Nan模块开发C++插件
阅读量:733 次
发布时间:2019-03-21

本文共 2341 字,大约阅读时间需要 7 分钟。

Electron框架下使用Nan模块开发C++插件

最近接到PC桌面端视频开发的任务,之前一直使用C++外加AngularJS的模式开发,感觉架构略显笨重,尤其是在修改C++时,可是相当的繁琐,成本太高,客户受不了了 recent发现了一个新玩意Electron,集成Vue又可以插入C++插件,使业务功能解耦。

网上关于Electron入门文档众多,本人慢慢搞定了,本文正记录在追加C++插件过程中遇到的坑,供入门者参考。

项目名下新建三个文件:package.json

{"name": "项目名","version": "1.0.0","description": "Electron的C++插件","main": "main.js","dependencies": {"electron": "^3.0.8"},"devDependencies": {"electron-packager": "^12.2.0"},"scripts": {"test": "npm test","start": "electron ."},"author": "yaohj","license": "ISC"}

main.js

const electron = require('electron')const app = electron.appconst BrowserWindow = electron.BrowserWindowconst path = require('path')const url = require('url')

let mainWindow

function createWindow() {mainWindow = new BrowserWindow({width: 800,height: 600})mainWindow.loadURL(url.format({pathname: path.join(__dirname, 'index.html'),protocol: 'file:',slashes: true}))mainWindow.webContents.openDevTools()mainWindow.on('closed', function() {mainWindow = null})}

app.on('ready', createWindow)app.on('window-all-closed', function() {if (process.platform !== 'darwin') {app.quit()}})

app.on('activate', function() {if (mainWindow === null) {createWindow()}})

index.html

项目目录下执行命令:electron . 或 npm start

开始追加C++插件代码,在项目名/native下新建C++程序

注:引用网络代码段

#include <nan.h>void Add(const Nan::FunctionCallbackInfov8::value& info) {if (info.Length() < 2) {Nan::ThrowTypeError("Wrong number of arguments");return;}if (!info[0]->IsNumber() || !info[1]->IsNumber()) {Nan::ThrowTypeError("Wrong arguments");return;}double arg0 = info[0]->NumberValue();double arg1 = info[1]->NumberValue();v8::Localv8::number num = Nan::New(arg0 + arg1);info.GetReturnValue().Set(num);}

void Init(v8::Localv8::object exports) {exports->Set(Nan::New("add").ToLocalChecked(),Nan::Newv8::functiontemplate(Add)->GetFunction());}

NODE_MODULE(demo, Init)

修改index.html:

项目目录下新建binding.gyp:

{"targets": [{"target_name": "demo","sources": ["native/demo.cc"],"include_dirs": ["<! zákonpirat тут>"]}]

编译运行环境配置

安装Python2.7(官网下载安装)安装node-gyp:npm install -g node-gyp安装non:npm install -g non

开始编译运行

node-gyp configurenode-gyp build(网上资料编译没说明,编译出的demo.node不能在electron中运行,只能在node.js调用)

node-gyp rebuild --target=3.0.8 --arch=x64 --(dist-url=https://atom.io(download/electron) (有时build不好用,试试用rebuild)

运行

electron .

另外,build目录下有binding.sln,可用Visual Studio打开 작성C++代码

总结过程中坑太多,上述是经过几百次试验的最简步骤

转载地址:http://adrgz.baihongyu.com/

你可能感兴趣的文章
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>
npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
查看>>
npm scripts 使用指南
查看>>
npm should be run outside of the node repl, in your normal shell
查看>>
npm start运行了什么
查看>>
npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
查看>>
npm 下载依赖慢的解决方案(亲测有效)
查看>>