nodejs addon c++ 连接其他库文件 node-gyp
1. 使用静态库
1, test.js
1 2 3 4 5 | var addon = require( './hello' ); var mpsse = addon.hello(); console.log(mpsse.x); |
2, hello.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <node.h> #include <v8.h> #include "NFCInfo.h" //外部静态库的头文件 using namespace v8; <br> //静态类用作全局变量,可以返回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;<br><br> //测试的外部静态类方法 NFCInfo nfc( "NFC" ); nfc.writeSmartposter( "zhibin" , "232323" );<br> 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
1 2 3 4 5 6 7 8 9 10 11 | { "targets" : [ { "target_name" : "hello" , "sources" : [ "hello.cpp" ], 'include_dirs' : [ './include' ] } ] } |
1 | include_dirs是外部库的头文件<br><br>4,mybuild.sh<br><br> |
1 2 | 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加入一句话:
1 2 3 4 5 6 7 8 9 10 11 12 | { "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
1 2 3 4 5 6 7 8 9 10 11 12 13 | { "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
标签:
nodejs
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?