1. 用 C++ Addons 實作出可在node執行的模組 (例如: hellocc.node 模組 )
參考: C++ Addons快速入門 (Using C++ Addons to call C/C++ code from Node.js)
2. clone electron-quick-start,快速建立可運行的Electron程式
# Clone this repository git clone https://github.com/electron/electron-quick-start # Go into the repository cd electron-quick-start # Install dependencies npm install # Run the app npm start
3. 將步驟1所建立成功的 binding.gyp 、hello.cc 移至 electron-quick-start 資料夾,並重新build hellocc.node 模組
並依hello.js的方式,將程式碼放入main.js去呼叫模組程式 hello( )
const addon = require('./build/Release/hellocc'); // 引入自訂模組
console.log(addon.hello()); // 呼叫模組程式 hello( ), 並印出傳回來的字串
# Rebuild hellocc.node cd electron-quick-start node-gyp configure node-gyp build # Run the app npm start
4. 然而並沒有執行成功,我會有NODE_MODULE_VERSION版本問題,錯誤狀況如下:
參考 Electron's native modules guide,安裝模組並重新建置 Electron
npm install --save-dev electron-rebuild # Install dependencies npm install # Every time you run "npm install", run this: ./node_modules/.bin/electron-rebuild # On Windows if you have trouble, try: .\node_modules\.bin\electron-rebuild.cmd # Run the app npm start
npm start後應該就可以成功印出cc了。
備註:若所用的C/C++程式需要引入動態/靜態library,在 binding.gyp加入指定"libraries",並以絕對路徑表示。
參考:Node native module - linking a static library
文章標籤
全站熱搜
