使用过node-gyp编译nodejs的addon插件的人,一定很好奇,node-gyp到底帮你做了什么事情,还有,如果我们自己做,难度到底如何。本文不作makefile文件语法的讲解,如果你不懂, 没关系,能看懂基本流程就好。抛开node-gyp,你会发现,有些复杂的东西其实是基于很简单的原理。
环境
操作系统为centos7,已经安装了nodejs,版本为0.10.36,其实差不多新的版本就行。编译链也已经准备好,g++命令啦。
编写代码
源文件hello.cc,很简单,基本和官网的hello world例子一样。
#include <stdio.h>
#include <stdlib.h>
#include <node/node.h>
#include <node/v8.h>
using namespace v8;
/**
* 输出world
*/
Handle<Value> hello(const Arguments& args)
{
HandleScope scope;
return scope.Close(String::New("world"));
}
void init(Handle<Object> exports) {
exports->Set(String::NewSymbol("hello"), FunctionTemplate::New(hello)->GetFunction());
}
NODE_MODULE(hello, init)
<