[笔记] Node.js本地扩展(Native Extension)

Windows下GYP编译很麻烦,于是在GitHub上找到了node-gyp,还是专门针对Node.js扩展编译配置生成工具。

首先是安装Visual Studio 2010 Express: http://www.microsoft.com/visualstudio/eng/downloads#d-2010-express

接着安装Python 2.7: http://www.python.org/getit/

下载Node.js: http://nodejs.org/download/

最后就是下载并解压node-gyp: https://github.com/TooTallNate/node-gyp (假设解压到c:/node-gyp/)

在 控制面板 中找到 系统,然后编辑 环境变量,使python和node都可以在命令行中直接使用。

创建一个新文件夹,然后按照 https://github.com/joyent/node/tree/master/test/addons 中HelloWorld的代码创建binding.cc, binding.gyp和test.js。

在写一个make.bat:

node.exe c:/node-gyp/bin/node-gyp.js configure
node.exe c:/node-gyp/bin/node-gyp.js build

运行过make.bat后,运行node test.js就可以了。


最基本的两个头文件是:node.h和v8.h

然后基本框架是:

Handle<Value> Method(const Arguments& args) {
  // do something
}

void init(Handle<Object> target) {
  NODE_SET_METHOD(target, "function_name", Method);
}

后面就是自行发挥的天地啦。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章