码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-03-22 01:08:20      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:fine   --   编程   hub   more   文件链接   简单   www   commit   

1. 软件准备

2. 实例

新建一个文件夹

  • package.json

    {
      "name": "test-cpp-module",
      "version": "0.1.0",
      "private": true,
      "gypfile": true,
      "dependencies": {
        "node-addon-api": "^2.0.0"
      }
    }
  • binding.gyp

    {
      "targets": [
        {
          "target_name": "democpp",
          "sources": [
            "democpp.cc"
          ],
          "include_dirs": [
            "<!@(node -p \"require('node-addon-api').include\")"
          ],
          "libraries": [ 
    
          ],
          "dependencies": [
            "<!(node -p \"require('node-addon-api').gyp\")"
          ],
          "cflags!": ["-fno-exceptions"],
          "cflags_cc!": ["-fno-exceptions"],
          "defines": ["NAPI_CPP_EXCEPTIONS"],
          "xcode_settings": {
            "GCC_ENABLE_CPP_EXCEPTIONS": "YES"
          }
        }
      ]
    }
  • democpp.cc

    #include <napi.h>
    
    using namespace Napi;
    
    String Hello(const CallbackInfo& info) {
      return String::New(info.Env(), "world");
    }
    
    Napi::Object  Init(Env env, Object exports) {
      exports.Set("hello", Function::New(env, Hello));
      return exports;
    }
    NODE_API_MODULE(addon, Init)

在当前文件夹开启命令行,保持网络畅通,运行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

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

标签:fine   --   编程   hub   more   文件链接   简单   www   commit   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!