2017-05-16 30 views
0

我正在使用本地C++在電子上製作簡單的hello世界應用程序,但得到這個Uncaught Error : error 1114錯誤。這個錯誤是專門在Windows上運行的項目,而它在Fedora上運行良好。未捕獲的錯誤:電子中的錯誤1114

Uncaught Error : error 1114 in electron

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> 

我已經配置,並建立工程幾次,但似乎並沒有被這種情況下很有幫助。

回答

2

首先,你必須在你的代碼的幾個缺陷:

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

其次,你必須在guide描述專門建立自己的插件來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安慰

成功運行

當你不使用你的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