擴展是動態(tài)鏈接的共享對象,可以與C 和C++ 庫鏈合。目前API 是相當(dāng)復(fù)雜,涉及數(shù)個庫的知識:
Node 靜態(tài)編譯所有組件成可執(zhí)行文件。當(dāng)您編譯您的模塊時,您不必考慮以上庫的連結(jié)。 制作一個小型擴展能達到以下效用(C++ 除外):
exports.hello = 'world'; 創(chuàng)建文件hello.cc:
#include <v8.h>
using namespace v8;
extern "C" void
init (Handle<Object> target)
{
HandleScope scope;
target->Set(String::New("hello"), String::New("World"));
}
此源文件需要編譯成hello.node(二進制擴展)。需要創(chuàng)建一個 python 文件wscript:
srcdir = '.'
blddir = 'build'
VERSION = '0.0.1'
def set_options(opt):
opt.tool_options('compiler_cxx')
def configure(conf):
conf.check_tool('compiler_cxx')
conf.check_tool('node_addon')
def build(bld):
obj = bld.new_task_gen('cxx', 'shlib', 'node_addon')
obj.target = 'hello'
obj.source = 'hello.cc'
執(zhí)行node-waf configure build 將會創(chuàng)建您的擴展文件至build/default/hello.node。 node-waf 是http://code.google.com/p/waf/[WAF],基於python 的編譯系統(tǒng)。node-waf 為使用者提供輕易。 所有Node 擴展必須輸出一函數(shù)init ,并包含此聲明: extern 'C' void init (Handle
至現(xiàn)時為止,此乃完整的擴展說明文件。請閱http://github.com/ry/node_postgres 以取得真實范例。
更多建議: