是否可以使用 c++ 作为 Electron.js 的后端?

2022-01-10 00:00:00 electron javascript c++

我的任务是制作简单的 c++ 应用程序,将信息存储到二进制文件中,然后需要对这些信息进行简单的操作,例如编辑、删除、读取.我想使用 Electron 创建桌面应用程序来设计 UI,并使用 c++ 来处理信息.

I have a task to make simple c++ app which stores information into binary files, then need to make simple manipulations with this info, like edit,delete,read. I wanted to create desktop app using Electron for designing UI and to use c++ for manipulation with information.

有可能吗,我如何将 c++ 包含到电子中,有教程吗?提前致谢.

Is it possible and how can i include c++ into electron, is there any tutorials? Thanks in advance.

推荐答案

Electron 使用的是 nodejs,所以你仍然可以将 cpp 代码打包为 node 模块,然后在你的电子应用程序中使用它作为依赖.

Electron is using nodejs, so you can still package cpp code as a node module and then use it as dependency in your electron app.

查看 Hello World 示例此处,它基本上是这样做的:

See the Hello World example here which basically does this:

module.exports.hello = () => 'world';

这是他们教程中的示例:

This is the example from their tutorial:

// hello.cc
#include <node.h>

namespace demo {

using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;

void Method(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();
  args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));
}

void init(Local<Object> exports) {
  NODE_SET_METHOD(exports, "hello", Method);
}

NODE_MODULE(addon, init)

}  // namespace demo

相关文章