C/C++实现nodejs扩展接口 node-addon-api

时间:2020-03-22 00:36:31   收藏:0   阅读:186

1. 软件准备

2. 实例

新建一个文件夹

在当前文件夹开启命令行,保持网络畅通,运行npm install

E:\WorkSpace\Code\node-addon-cplus>npm install

> test-cpp-module@0.1.0 install E:\WorkSpace\Code\node-addon-cplus
> node-gyp rebuild


E:\WorkSpace\Code\node-addon-cplus>if not defined npm_config_node_gyp (node "D:Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin\\.
.\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node "D:\Program Fi
les\nodejs\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" rebuild )
Building the projects in this solution one at a time. To enable parallel build,
please add the "/m" switch.
  nothing.c
  win_delay_load_hook.cc
  nothing.vcxproj -> E:\WorkSpace\Code\node-addon-cplus\build\Release\\nothing.
  lib
  democpp.cc
  win_delay_load_hook.cc
e:\workspace\code\node-addon-cplus\node_modules\node-addon-api\napi-inl.h(2141)
: warning C4530: C++ exception handler used, but unwind semantics are not enabl
ed. Specify /EHsc (compiling source file ..\democpp.cc) [E:\WorkSpace\Code\node
-addon-cplus\build\democpp.vcxproj]
     Creating library E:\WorkSpace\Code\node-addon-cplus\build\Release\democpp.
  lib and object E:\WorkSpace\Code\node-addon-cplus\build\Release\democpp.exp
  democpp.vcxproj -> E:\WorkSpace\Code\node-addon-cplus\build\Release\\democpp.
  node
npm notice created a lockfile as package-lock.json. You should commit this file.

added 1 package from 50 contributors and audited 1 package in 15.303s
found 0 vulnerabilities


E:\WorkSpace\Code\node-addon-cplus>

顺利编译,此时在得到输出文件.\build\Release\democpp.node,下一步,在node中调用hello()

E:\WorkSpace\Code\node-addon-cplus>node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> testapp=require('./build/Release/democpp.node')
{ hello: [Function] }
> testapp.hello()
'world'
>

提供上述实例源文件链接

参考文章

[1] 简单上手nodejs调用c++(c++和js的混合编程)

[2] node-addon-examples

原文:https://www.cnblogs.com/macrored/p/12543462.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!