nodejs调用c++生成的动态库dll例子,要求node >= 15.0.0
不懂的可以微信我号yizheng369

源码地址 https://gitee.com/618859/nodejs_dll

1.环境总结

1.要安装Visual Studio 2019
2.node v16.14.0或以上
3.安装python-2.7.15,并配置他的环境变量
4.全局安装node-gyp v9.0.0
5.全局安装最新的windows-build-tools
6.最后在项目里安装最新的ffi-napi
以上6步都完成后,就可以用node调用dll文件了

环境安装

  1. 安装 Visual Studio 2019 下载地址https://my.visualstudio.com/Downloads?q=Visual%20Studio%202019

install

npm install

run

node ffi.js

详细安装过程

PS D:\pratDemo\node_demo\node_dll> node -v
v16.14.0
PS D:\pratDemo\node_demo\node_dll> npm -v
8.3.1
PS D:\pratDemo\node_demo\node_dll> npm i
npm WARN old lockfile
npm WARN old lockfile The package-lock.json file was created with an old version of npm,
npm WARN old lockfile so supplemental metadata must be fetched from the registry.
npm WARN old lockfile
npm WARN old lockfile This is a one-time fix-up, please be patient...
npm WARN old lockfile
npm ERR! code 1
npm ERR! path D:\pratDemo\node_demo\node_dll\node_modules\ffi-napi
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node-gyp-build
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@8.4.1
npm ERR! gyp info using node@16.14.0 | win32 | x64
npm ERR! gyp info find Python using Python version 3.10.4 found at "C:\Users\80455\AppData\Local\Programs\Python\Python310\python.exe"
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS msvs_version not set from command line or npm config
npm ERR! gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt

上面很明显是安装失败了,但下面有很多提示信息,我们耐心看

npm ERR! gyp ERR! find VS checking VS2019 (16.11.32413.511) found at: 

提示1:这里提示我们要安装 VS2019,就是 Visual Studio 2019
因为我们用nodejs调用dll时,需要VS2019的一些插件来帮助才能实现,故要安装。你也可以安装 Visual Studio 2013 或者Visual Studio 2015都行

npm ERR! gyp ERR! find VS "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community"
npm ERR! gyp ERR! find VS - found "Visual Studio C++ core features"
npm ERR! gyp ERR! find VS - missing any VC++ toolset
npm ERR! gyp ERR! find VS could not find a version of Visual Studio 2017 or newer to use
npm ERR! gyp ERR! find VS looking for Visual Studio 2015
npm ERR! gyp ERR! find VS - not found
npm ERR! gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS You need to install the latest version of Visual Studio
npm ERR! gyp ERR! find VS including the "Desktop development with C++" workload.
npm ERR! gyp ERR! find VS For more information consult the documentation at:

提示2:你可以查看这个网址,但是我打不开

npm ERR! gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows 

这是一段中间的报错信息

npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Could not find any Visual Studio installation to use
npm ERR! gyp ERR! stack     at VisualStudioFinder.fail (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:122:47)
npm ERR! gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:75:16
npm ERR! gyp ERR! stack     at VisualStudioFinder.findVisualStudio2013 (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:363:14)
npm ERR! gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:71:14
npm ERR! gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:384:16
npm ERR! gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\util.js:54:7
npm ERR! gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\util.js:33:16
npm ERR! gyp ERR! stack     at ChildProcess.exithandler (node:child_process:406:5)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:520:28)
npm ERR! gyp ERR! stack     at maybeClose (node:internal/child_process:1092:16)
npm ERR! gyp ERR! System Windows_NT 10.0.22000
npm ERR! gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd D:\pratDemo\node_demo\node_dll\node_modules\ffi-napi
npm ERR! gyp ERR! node -v v16.14.0
npm ERR! gyp ERR! node-gyp -v v8.4.1
npm ERR! gyp ERR! not ok

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\80455\AppData\Local\npm-cache\_logs\2022-05-25T13_35_06_797Z-debug-0.log

提示3:以上是报错信息,但从报错信息里面,我们能看到一些头绪
后来我直接百度,在百度里搜索: https://github.com/nodejs/node-gyp
搜到这篇文章:https://www.jianshu.com/p/7148b27eb61e

其实我之前成功安装过一次,我知道要安装 windows-build-tools
刚好上面文章的那个网友也说到要安装,那就理由比较充分了,那就安装嘛。
所以我就输入 npm i -g windows-build-tools

PS D:\pratDemo\node_demo\node_dll> npm i -g windows-build-tools
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated windows-build-tools@5.2.2: Node.js now includes build tools for Windows. You probably no longer need this tool. See https://github.com/felixrieseberg/windows-build-tools for details.
npm ERR! code 3221225786
npm ERR! path C:\Users\80455\AppData\Roaming\npm\node_modules\windows-build-tools
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node ./dist/index.js

**提示4:**报错信息里面有:这一句话非常经典,他提示我们需要安装python 2.7.15版本

Downloading python-2.7.15.amd64.msi  

于是我就去python官网下载 https://www.python.org/downloads/
最后我挑了python 2.7.5这个版本,也适合了,就这个地址:https://www.python.org/downloads/release/python-275/
下载好直接双击安装就好
安装过程中,默认有一个安装地址是 C:\Python27
那你不要修改,一直点击下一步就好

等python 2.7.5安装好后,你要为python配置环境变量

配置环境变量文本教程:https://blog.csdn.net/yuan_618859/article/details/124975498?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22124975498%22%2C%22source%22%3A%22yuan_618859%22%7D&ctrtid=SyFIz

配置环境变量视频教程:https://www.bilibili.com/video/BV1WE411v7mN/

接下来:

npm ERR! [============================================>] 100.0% of 20.2 MB (653 kB/s)
npm ERR! Downloaded python-2.7.15.amd64.msi. Saved to C:\Users\80455\.windows-build-tools\python-2.7.15.amd64.msi.
Downloading vs_BuildTools.exe
npm ERR! [============================================>] 100.0% of 1.12 MB (299 kB/s)
npm ERR! Downloaded vs_BuildTools.exe. Saved to C:\Users\80455\.windows-build-tools\vs_BuildTools.exe.
npm ERR!
npm ERR! Starting installation...
npm ERR! Launched installers, now waiting for them to finish.
npm ERR! This will likely take some time - please be patient!
npm ERR!
npm ERR! Status from the installers:
---------- Visual Studio Build Tools ----------
npm ERR! Still waiting for installer log file...
npm ERR! ------------------- Python --------------------
npm ERR! Successfully installed Python 2.7
npm ERR! ^C

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\80455\AppData\Local\npm-cache\_logs\2022-05-25T13_43_14_454Z-debug-0.log

到这里,我们再次输入 npm i
试图安装项目依赖,却发现还是有错误

PS D:\pratDemo\node_demo\node_dll> npm i
npm WARN old lockfile
npm WARN old lockfile The package-lock.json file was created with an old version of npm,
npm WARN old lockfile so supplemental metadata must be fetched from the registry.
npm WARN old lockfile
npm WARN old lockfile This is a one-time fix-up, please be patient...
npm WARN old lockfile
npm ERR! code 1
npm ERR! path D:\pratDemo\node_demo\node_dll\node_modules\ffi-napi
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node-gyp-build
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@8.4.1

提示4: 从上一句npm ERR! gyp info using node-gyp@8.4.1
我们知道还需要安装node-gyp
然后:我们通过上文搜到的这篇文章:https://www.jianshu.com/p/7148b27eb61e
也提到我们需要安装node-gyp
所以我们输入 npm install -g node-gyp

PS D:\pratDemo\node_demo\node_dll> npm install -g node-gyp

added 94 packages, and audited 95 packages in 12s

5 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

到这里,没有报错信息,所有环境都安装好了,
最后我们输入 npm i
安装 package.json里面准备好的 ffi-napi

PS D:\pratDemo\node_demo\node_dll> npm i
npm WARN old lockfile
npm WARN old lockfile The package-lock.json file was created with an old version of npm,
npm WARN old lockfile so supplemental metadata must be fetched from the registry.
npm WARN old lockfile
npm WARN old lockfile This is a one-time fix-up, please be patient...
npm WARN old lockfile

added 1 package, removed 25 packages, changed 9 packages, and audited 11 packages in 29s

found 0 vulnerabilities

这里提示安装项目的依赖成功!

最后我们运行项目,成功:

PS D:\pratDemo\node_demo\node_dll> node .\ffi.js
结果: 9
PS D:\pratDemo\node_demo\node_dll>
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐