[筆記] 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);
}

後面就是自行發揮的天地啦。


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