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