How to resolve LoadError: cannot load such file -- ffi_c

How to resolve LoadError: cannot load such file -- ffi_c

我想知道如何解决安装 Ruby 2.2.1 Windows 安装程序和 Ruby gem 2.4.6.[=14 后在控制台上执行 require 命令时出现的下一个错误=]

LoadError: cannot load such file -- ffi_c
        from C:/Ruby22-x64/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_req
uire.rb:54:in `require'

这像 DLL 吗?

如果您阅读 ffirequirement 文档,您可以看到:

You need a sane building environment in order to compile the extension. At a minimum, you will need:

  • A C compiler (e.g. Xcode on OSX, gcc on everything else)
  • libffi development library - this is commonly in the libffi-dev or libffi-devel

这意味着gem没有预编译,安装时必须编译代码。这反过来意味着,如果您 运行 在 PC 上,则需要为 windows 安装 Ruby 开发工具包,也就是 ruby​​installer 站点上的“devkit', you can get it from the downloads page

先下载并安装devkit,然后打开一个新的命令行window,然后是:

gem install ffi

有关详细信息,请参阅此 SO:

我认为 ffi 在 C:\Ruby22-x64\lib\ruby\gems.2.0\gems\ffi-1.9.8-x64-mingw32\lib\ffi.rb 中的正则表达式中有一个小错误,这使得它错误地认为你是 运行 ruby 2.1.x

它测试 RUBY_VERSION =~ /2.1/ 捕获 '2.2.1' 而它应该测试 RUBY_VERSION =~ /^2.1/ 行首字符在.

应该是:

if RUBY_VERSION =~ /^1\.8/
      require '1.8/ffi_c'
    elsif RUBY_VERSION =~ /^1\.9/
      require '1.9/ffi_c'
    elsif RUBY_VERSION =~ /^2\.0/
      require '2.0/ffi_c'
    elsif RUBY_VERSION =~ /^2\.1/
      require '2.1/ffi_c'
    elsif RUBY_VERSION =~ /^2\.2/
      require '2.2/ffi_c'
    else
      require 'ffi_c'
    end

我看到它现在已经修复了https://github.com/ffi/ffi/commit/4168ef3dbd56a7b52978efb2ff7d0dc448f8f8f1

我得到的整个错误如下,但是在 Mac:

/Users/mayuresh.srivastava/.rvm/gems/ruby-2.7.3/gems/ffi-1.15.4/lib/ffi.rb:3:in `require': cannot load such file -- 2.7/ffi_c (LoadError)

/Users/mayuresh.srivastava/.rvm/gems/ruby-2.7.3/gems/ffi-1.15.4/lib/ffi.rb:5:in `require': dlopen(/Users/mayuresh.srivastava/.rvm/gems/ruby-2.7.3/gems/ffi-1.15.4/lib/ffi_c.bundle, 9): Library not loaded: /opt/homebrew/opt/libffi/lib/libffi.7.dylib (LoadError)
  Referenced from: /Users/mayuresh.srivastava/.rvm/gems/ruby-2.7.3/gems/ffi-1.15.4/lib/ffi_c.bundle
  Reason: image not found - /Users/mayuresh.srivastava/.rvm/gems/ruby-2.7.3/gems/ffi-1.15.4/lib/ffi_c.bundle

我检查了 ffi,它已经存在了:

mayuresh.srivastava$ gem list ffi

*** LOCAL GEMS ***

ffi (1.15.4)
public_suffix (4.0.6)

我还是重新安装了 ffi,它成功了。

mayuresh.srivastava$ gem install ffi
Building native extensions. This could take a while...
Successfully installed ffi-1.15.4
Parsing documentation for ffi-1.15.4
Installing ri documentation for ffi-1.15.4
Done installing documentation for ffi after 11 seconds
1 gem installed