1.環境
Ubuntu 14.04
node 4.5.0
node-gyp 3.4.0
2.項目
新建項目,在項目文件的node_modules中加入組件nan和bindings;或者在package.json的dependencies中加入這兩個組件,用nmp安裝
3.c++源碼
#include <nan.h>void Method(const Nan::FunctionCallbackInfo<v8::Value>& info) { info.GetReturnValue().Set(Nan::New("world").ToLocalChecked());}void Init(v8::Local<v8::Object> exports) { exports->Set(Nan::New("hello").ToLocalChecked(), Nan::New<v8::FunctionTemplate>(Method)->GetFunction());}NODE_MODULE(hello, Init)4.binding.gyp{ "targets": [ { "target_name": "hello", "sources": [ "hello.cc" ], "include_dirs": [ "<!(node -e /"require('nan')/")" ] } ]}5.js源碼var addon = require('bindings')('hello');console.log(addon.hello()); // 'world'6.編譯addonnode-gyp configure build7.執行node hello.js
新聞熱點
疑難解答
圖片精選