簡體   English   中英

未捕獲錯誤:電子錯誤1114

[英]Uncaught Error : error 1114 in electron

我正在使用本機C ++在電子上做一個簡單的hello world應用程序,但是遇到了這個Uncaught Error : error 1114錯誤。 當項目在Windows上運行而在Fedora上運行良好時,此錯誤特別存在。

未捕獲錯誤:電子錯誤1114

package.json

{
    "name": "nodec",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "start": "electron ."
    },
    "author": "",
    "license": "ISC",
    "devDependencies": {
        "electron-packager": "^8.7.0"
    }
}

binding.gyp

{
    "targets": [
        {
            "target_name": "addon",
            "sources": [ "addon.cc" ]
        }
    ]
}

addon.cc

#include <node.h>
namespace demo {
using v8::Exception;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::String;
using v8::Value;

void hello(const FunctionCallbackInfo& args) {
Isolate* isolate = args.GetIsolate();

args.GetReturnValue().Set(String::NewFromUtf8(isolate,"world"));
}

void Init(Local exports) {
NODE_SET_METHOD(exports, "hello", hello);
}

NODE_MODULE(addon, Init)
}

main.js

const addon = require('./build/Release/addon');
console.log('This should be eight:', addon.hello());

index.html

<title>My C++ App</title> Hello <script> require('./main.js') </script>

我已經多次配置和構建了項目,但是在這種情況下似乎沒有幫助。

首先,您的代碼中存在幾個缺陷:

  • addon.ccFunctionCallbackInfoLocal必須有模板參數。 更正的功能簽名為:
void hello(const FunctionCallbackInfo<Value>& args)
void Init(Local<Object> exports)
  • package.json :您的入口點應該是
"main": "main.js",

其次,您必須按照指南中所述專門針對electron構建附加物。 例如,使用以下命令將其構建為最新的electron版本(1.4.13):

node-gyp configure build --target=1.4.13 --arch=x64 --dist-url=https://atom.io/download/electron

--arch標志根據您的平台而定)

完成所有這些操作后,它可以成功運行

npm run start

打印This should be eight: world控制台This should be eight: world


由於您不在代碼中的任何地方使用index.html盡管您的目標可能是在此處打印-您可以嘗試使用這些改進的main.jsindex.html

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

app.once('ready', () => {
  new BrowserWindow().loadURL(path.join(__dirname, 'index.html'))
})
<html>
  <head>
    <title>My C++ App</title>
  </head>
  <body>
    <div>
      <h1>
        Hello 
        <script>document.write(require('./build/Release/addon').hello())</script>
      </h1>
    </div>
  </body>
</html>

結果在瀏覽器窗口中顯示Hello world

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM