关于nodejs的官方文档网址:http://nodejs.org/
关于在nodejs下编译c++模块代码也就是nodejs下c++ addons开发的官方介绍网址:http://nodejs.org/api/addons.html
nodejs采用node-gyp的方式编译,node-gyp的代码下载和使用介绍网址:https://github.com/TooTallNate/node-gyp
首先介绍一下背景:v8是一个解释执行Javascript脚本的程序,可以在c++中调用v8的api执行javascript代码同时在javascript与c++之间实现变量,函数,对象的传递。nodejs是一个基于v8的javascript程序开发框架。可以基于node开发javascript代码,支持在javascript调用c++代码及在c++代码中调用javascript代码。javascript是不用编译的,但是javascript想调用的c++代码需要提前先编译,nodejs采用node-gyp集成编译工具来编译c/c++库。
安装和编译步骤:
1.下载nodejs源码并编译(./configure --> make --> make install)注意不要用apt-get,貌似他没有把头文件搞下来,我采用的是默认的安装路径,后来编译c++模块的时候报错很多找不到头文件,find命令发现其实这些文件已经有了,只是不在编译器能看到的路径下,所以将对应的文件拷贝到编译器可以找到的路劲下或者增加到环境变量,总之,怎么方便怎么来,对于头文件都是这样的。
2.安装其他的依赖比如:npm,node-gyp,这些都可以直接通过apt-get非常快。
3.demo编译一下,直接摘自官方文档啦(注意版本一定要对应,我开始就遇到网上摘下来的一段demo代码,结果编译各种报错,建议官方版本,官方下载demo):
最最关键的:node的版本在不断变化,哪怕官方网站的demo可能也是老版本的,其实他真是这样子的,然后我还搞了好一会,建议官网上有这么一句:All of the following examples are available for download and may be used as a starting-point for your own Addon.,直接到download里面去下载吧,别问我为什么,node太任性!
git网址:https://github.com/rvagg/node-addon-examples
Hello world#
To get started let's make a small Addon which is the C++ equivalent of the following JavaScript code:
module.exports.hello = function() { return 'world'; };
First we create a file hello.cc
:
#include <node.h>
#include <v8.h>
using namespace v8;
Handle<Value> Method(const Arguments& args) {
HandleScope scope;
return scope.Close(String::New("world"));
}
void init(Handle<Object> exports) {
exports->Set(String::NewSymbol("hello"),
FunctionTemplate::New(Method)->GetFunction());
}
NODE_MODULE(hello, init)
Note that all Node addons must export an initialization function:
void Initialize (Handle<Object> exports);
NODE_MODULE(module_name, Initialize)
There is no semi-colon after NODE_MODULE
as it's not a function (see node.h
).
The module_name
needs to match the filename of the final binary (minus the .node suffix).
The source code needs to be built into hello.node
, the binary Addon. To do this we create a file calledbinding.gyp
which describes the configuration to build your module in a JSON-like format. This file gets compiled by node-gyp.
{
"targets": [
{
"target_name": "hello",
"sources": [ "hello.cc" ]
}
]
}
The next step is to generate the appropriate project build files for the current platform. Use node-gyp configure
for that.
Now you will have either a Makefile
(on Unix platforms) or a vcxproj
file (on Windows) in the build/
directory. Next invoke the node-gyp build
command.
Now you have your compiled .node
bindings file! The compiled bindings end up in build/Release/
.
You can now use the binary addon in a Node project hello.js
by pointing require
to the recently builthello.node
module:
var addon = require('./build/Release/hello');
console.log(addon.hello()); // 'world'
即在同一个目录下创建上面4个文件hello.cc binding.gyp hello.js package.json。
4.在目录下执行:
node-gyp configure5.编译c++库:
$ node-gyp build编译结果在 build/Release/ 目录下hello.node
好了,现在可以在js中调用这个c++库了
5.执行node hello.js可以看到在c++中返回来的字符串。
(注:据说hello.node就是一个普通的c++库,只是把之前的.so改回.node,在nodejs就是通过flopen()函数调用这个库的,这个后面再详细研究)