1.环境
Ubuntu 14.04
node 4.5.0
node-gyp 3.4.0
2.c++源码
// hello.cc
#include <node.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, "hello world"));
}
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "hello", Method);
}
NODE_MODULE(addon, init)
} // namespace demo
3.build file(binding.gyp)
{
"targets": [
{
"target_name": "addon",
"sources": [ "hello.cc" ]
}
]
}
4.js代码
// hello.js
const addon = require('./build/Release/addon');
console.log(addon.hello());
// Prints: 'hello world'
5.编译addon
node-gyp configure build
6.执行程序
node hello.js