1. 使用静态库
1, test.js
var addon = require('./hello'); var mpsse = addon.hello(); console.log(mpsse.x);
2, hello.cpp
#include <node.h> #include <v8.h> #include "NFCInfo.h" //外部静态库的头文件 using namespace v8;
//静态类用作全局变量,可以返回js对象。 class JSNFC{ public: static void init(Handle<Object> target){ nfcObj = Object::New(); nfcObj->Set(String::NewSymbol("x"), Integer::New(10) ); } static Handle<Object> nfcObj; }; Handle<Object> JSNFC::nfcObj; Handle<Value> Method(const Arguments& args) { HandleScope scope;
//测试的外部静态类方法 NFCInfo nfc("NFC"); nfc.writeSmartposter("zhibin","232323");
return scope.Close( JSNFC::nfcObj ); } void init(Handle<Object> target) { target->Set(String::NewSymbol("hello"), FunctionTemplate::New(Method)->GetFunction()); JSNFC::init(target); } NODE_MODULE(hello, init)
3,binding.gyp
{ "targets": [ { "target_name": "hello", "sources": [ "hello.cpp" ], 'include_dirs': [ './include' ] } ] }
include_dirs是外部库的头文件
4,mybuild.sh
node-gyp configure build gcc -fexceptions -O2 -o hello.node ./build/Release/obj.target/hello/hello.o ./libmynfclib.a /usr/local/lib/libnfc.a -shared -fPIC
在当前目录生成了 hello.node 可执行动态库。
执行node-gyp test.js.
或者只要在binding.gyp加入一句话:
{
"targets": [
{
"target_name": "hello",
"sources": [ "hello.cpp" ],
'include_dirs': [
'./include'
],
"libraries":["/home/user/workspace/nodejs/libpanda.a", "/usr/local/lib/libnfc.a"]
}
]
}
2.使用动态库,避免两个静态库有redefine。
1)binding.gyp
{
"targets": [
{
"target_name": "hello",
"sources": [ "hello.cpp" ],
'include_dirs': [
'./include'
],
"libraries":["/home/user/workspace/nodejs/libpanda.a", "/usr/local/lib/libnfc.so","/usr/local/lib/libmpsse.so"],
"cflags!":["-fexceptions"]
}
]
}
2) hello.cpp
1 #include <node.h> 2 #include <v8.h> 3 4 #include "NFCInfo.h" 5 #include "AccelerMeter.h" 6 7 #define WIRING_DEFINE_CONSTANT(NAME, VALUE) (target)->Set( 8 v8::String::NewSymbol(NAME), 9 v8::Integer::New(VALUE), 10 static_cast<v8::PropertyAttribute>(v8::None) 11 ); 12 13 using namespace v8; 14 15 class JSNFC { 16 public: 17 static void init(Handle<Object> target) { 18 //Here can store target object. 19 } 20 21 static Handle<Object> readADXL() { 22 23 AccelerMeter am("AccelerMeter"); 24 am.readADXL(); 25 Handle<Object> mpsseObj = Object::New(); 26 mpsseObj->Set(String::NewSymbol("x"), Integer::New(am.x)); 27 mpsseObj->Set(String::NewSymbol("y"), Integer::New(am.y)); 28 mpsseObj->Set(String::NewSymbol("z"), Integer::New(am.z)); 29 30 return mpsseObj; 31 } 32 33 }; 34 35 Handle<Value> sendSmartposter(const Arguments& args) { 36 HandleScope scope; 37 38 NFCInfo nfc("NFC"); 39 nfc.writeSmartposter("zhibin", "232323"); 40 bool ret = nfc.send(); 41 return scope.Close(Integer::New((ret?1:0))); 42 } 43 44 Handle<Value> readADXL(const Arguments& args) { 45 HandleScope scope; 46 ; 47 return scope.Close(JSNFC::readADXL()); 48 } 49 50 void init(Handle<Object> target) { 51 target->Set(String::NewSymbol("smartposter"), FunctionTemplate::New(sendSmartposter)->GetFunction()); 52 target->Set(String::NewSymbol("readADXL"), FunctionTemplate::New(readADXL)->GetFunction()); 53 54 JSNFC::init(target); 55 } 56 NODE_MODULE(hello, init)
3) test.js
1 //var addon = require('./hello'); 2 var addon = require('./build/Release/hello'); 3 4 5 6 var mpsse = addon.readADXL(); 7 8 console.log(mpsse.x); 9 console.log(mpsse.y); 10 console.log(mpsse.z); 11 12 console.log(addon.smartposter());
4) 编译运行:
node-gyp build
node test.js