PS:請先升級Node 6.2.1,Node 升級命令 npm install -g n;n stable
.NOde.js擴展是一個經過C/C++編寫的動態連接庫,並經過Node.js的函數require()函數加載,用起來就像使用一個普通的Node.js模塊。它主要爲Node與C/C++庫之間提供接口。
這樣,若一個方法或函數是經過Node擴展實現則變得至關複雜,涉及幾個模塊與接口的知識:javascript
node:ObjectWrap
deps
目錄。詳情請見·Node.js's own dependencies for additional information。點我Node.js官方擴展庫示例,這也許是你爲Node.js編寫C/C++擴展庫的起點。只有V8和OpenSSL類常常在Node C/C++擴展中頻繁的使用。該示例適用Node.js版本號爲V5.0以上。html
// hello.js const addon = require('./build/Release/addon'); console.log(addon.hello()); // 'world'
// hello.cc #include <node.h> #include <v8.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
// binding.gyp { "targets": [ { "target_name": "addon", "sources": [ "hello.cc" ] } ] }
node-gyp命令java
node-gyp configure build