当前位置:首页 >> 网络编程

Node.js Addons翻译(C/C++扩展)

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扩展实现则变得相当复杂,涉及几个模块与接口的知识:

"htmlcode">

// 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命令

复制代码 代码如下:
node-gyp configure build

以上所述是小编给大家介绍的Node.js Addons翻译(C/C++扩展)的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!