1,安装必要的库
a)NodeJs
b)python 2.x
c)安装NASM
最新版地址: https://www.nasm.us/pub/nasm/releasebuilds/2.13.03/
并如b) 添加环境变量
2,编译Nodejs
3,配置nodejs环境变量
这里最好是通过我的电脑->属性->环境变量的方法设置环境变量,在cmd中配置环境变量只是暂时有效。
4,创建VS工程,并设置项目配置(主要是引用node库)
5,添加工程代码(以github helloworld为例):
a)binding.gyp
这里主要是用于GYP编译使用(例如xcode编译),如果是VS编译可以不用管这个文件。 声明targetname(插件名) 和source( 源cpp文件)
{
"targets": [
{
"target\_name": "helloworld",
"sources": [ "hello.cpp" ]
} ]
}
b)hello.cpp
源文件
// 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, "world"));
}
void init(Local<Object> exports) {
NODE\_SET\_METHOD(exports, "hello", Method);
}
NODE\_MODULE(addon, init)
} // namespace demo
c)package.json(包含模块的一些信息)
main很重要,标识了模块的路径,路径错了,就加载不了
{
"name": "hellpworld",
"version": "1.0.0",
"main": "../x64/Debug/helloworld"
}
d)run.js
js 代码,作为一个参数传给node.exe 的,helloworld 为模块名
var helloworld = require('../x64/Debug/helloworld');
console.warn(helloworld.hello());
setTimeout(function () {alert(2)}, 100000);
相关资料可查看:
nodejs addon 中文网: http://nodejs.cn/api/addons.html
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。