使用node-addon-api编写node.js插件

直接上干货。

步骤:

第一步:编写binding.gyp文件,参考node-gyp文档 node-gyp - npm

{
  "targets": [
    {
      "target_name": "ShareMemory",
      "cflags!": [ "-fno-exceptions" ],
      "cflags_cc!": [ "-fno-exceptions" ],
      "sources": [ "./mmap.cc" ],
      "include_dirs": [
        "<!@(node -p \"require('node-addon-api').include\")"
      ],
      'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
    }
  ]
}

第二步:编写C++代码,参考node-addon-api文档 node-addon-api - npm

第三步:安装node-gyp,参考node-gyp文档

npm install -g node-gyp

第四步:安装Python3

第五步:构建工程文件,参考node-gyp文档 

node-gyp configure --python "C:\Python310\python.exe"

构建的时候,可以添加构建参数,比如我需要编译windows 32位版本,那么可以加入参数 --arch=ia32 ,还有其他参数,可参见node-gyp文档

node-gyp configure --python "C:\Python310\python.exe" --arch=ia32

第六步:编译工程,生产.node文件

node-gyp build --python "C:\Python310\python.exe"

废话不多说,搞定!

猜你喜欢

转载自blog.csdn.net/ctbinzi/article/details/121957843