likes
comments
collection
share

node 调用 rust 代码

作者站长头像
站长
· 阅读数 26

Update

  • nodejs-sys 已经过时(只支持 node v15 之前的内容,因为napi更新了)。
  • node-ffi (ffi)已经过时(最新的提交在19年……比上面的还古老)。
  • 请使用 ffi-napi

使用 ffi-napi


原文章

TLDR: 如果你折腾过cpp的编译环境,这篇并不适合你。如果不想折腾或者是新手,这篇就很适合

内容只包含配置,如何在 rust 里编写 addon,LogRocket 有篇很好的文章

  1. 下载 rust for windows
  2. 编写代码(但你会碰到问题,这也是这篇文章的目的)
  3. 问题一:crates.io connection failed

  4. 问题二:cargo build 时报错:couldn't find any valid shared libraries matching: ['clang.dll', 'libclang.dll'], set the 'LIBCLANG_PATH' environment variable to a path where one of these files can be found (invalid: [])

    • 解决:报错很明显,是缺少 clang 相关的配置。稍作搜索可知,这其实是 rust 的 bindgen 需要 llvm+clang 来编译代码
    • 有三种解决办法:

      1. 自己去 llvm release 下载预编译好的文件,但是此版本需要 mingw
      2. 推荐阅读/blog,自己编译一下 llvm 的代码
      3. (其实只有两种,第一种是依赖 mingw,这种是靠 msvc)。稍作搜素,StackOverflow 上有提示,于是去 zig 的 wiki 下载

        1. 在 "Option 2: Using CMake and Microsoft Visual Studio" 章节找,llvm+clang 开头的下载链接下载
        2. 下载完后,将对应目录下的 bin 文件夹添加到 windows path。
        3. clang -v 成功,解决。
  5. cargo build --release 成功
转载自:https://segmentfault.com/a/1190000041356488
评论
请登录