#include <node.h>
#include <v8.h>
using namespace v8;
// 这里是 hello 函数的 C++ 实现部分
Handle<Value> Method(const Arguments& args) {
HandleScope scope;
return scope.Close(String::New("world"));
}
// 这里是模块的初始化函数,必须有
void init(Handle<Object> exports) {
exports->Set(String::NewSymbol("hello"),
FunctionTemplate::New(Method)->GetFunction());
}
// 这里定义本模块的名字和初始化函数
NODE_MODULE(hello, init)
exports.hello = function() {
return 'world';
};
为了编译 C++ 这个模块,还需要一个 JSON 格式的 binding.gyp 文件,来定义编译的细节。
{
"targets": [
{
"target_name": "hello",
"sources": [ "hello.cpp" ]
}
]
}
node test.js:
var addon = require('./build/Release/hello');
console.log(addon.hello());
void Initialize (Handle<Object> exports); NODE_MODULE(module_name, Initialize)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有