安装 CocoaPods 时出错 - macOS BigSur

Error installing CocoaPods - macOS BigSur

我正在学习如何编程,我正在尝试安装 Cocoapods,但我遇到了 sudo gem install cocoapods 错误。

这是我得到的:

    Building native extensions. This could take a while...
ERROR:  Error installing cocoapods:
    ERROR: Failed to build gem native extension.

    current directory: /Library/Ruby/Gems/2.6.0/gems/ffi-1.13.1/ext/ffi_c
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby -I /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0 -r ./siteconf20201006-615-fryp4a.rb extconf.rb
checking for ffi.h... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/$(RUBY_BASE_NAME)
    --with-ffi_c-dir
    --without-ffi_c-dir
    --with-ffi_c-include
    --without-ffi_c-include=${ffi_c-dir}/include
    --with-ffi_c-lib
    --without-ffi_c-lib=${ffi_c-dir}/lib
    --enable-system-libffi
    --disable-system-libffi
    --with-libffi-config
    --without-libffi-config
    --with-pkg-config
    --without-pkg-config
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:467:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:585:in `block in try_compile'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:534:in `with_werror'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:585:in `try_compile'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:1109:in `block in have_header'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:959:in `block in checking_for'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:361:in `block (2 levels) in postpone'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:331:in `open'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:361:in `block in postpone'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:331:in `open'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:357:in `postpone'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:958:in `checking_for'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:1108:in `have_header'
    from extconf.rb:10:in `system_libffi_usable?'
    from extconf.rb:42:in `<main>'

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-20/2.6.0/ffi-1.13.1/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Library/Ruby/Gems/2.6.0/gems/ffi-1.13.1 for inspection.
Results logged to /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-20/2.6.0/ffi-1.13.1/gem_make.out

我知道有一行说“你必须先安装开发工具”,一开始确实如此。然后我安装了 Xcode 命令行工具(我不得不从 Apple Developer 手动下载它,因为 xcode-select --install 行不起作用),并再次尝试安装 cocoapods。但是即使在重新启动后我也遇到了同样的错误。

它还有日志的路由,这是它所说的:

package configuration for libffi is not found
"xcrun clang -o conftest -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/universal-darwin20 -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/ruby/backward -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0 -I. -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT    -g -Os -pipe -DHAVE_GCC_ATOMIC_BUILTINS -DUSE_FFI_CLOSURE_ALLOC conftest.c  -L. -L/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib -L. -L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.Internal.sdk/usr/local/lib     -lruby.2.6   "
In file included from conftest.c:1:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/ruby.h:33:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/ruby/ruby.h:24:10: fatal error: 'ruby/config.h' file not found
#include "ruby/config.h"
         ^~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/ruby/ruby.h:24:10: note: did not find header 'config.h' in framework 'ruby' (loaded from '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks')
1 error generated.
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

所以,我卡住了。 我目前使用的是 macOS Big Sur Beta 9 和 Xcode 12.0.1(非测试版)。

嗯,今天终于可以成功安装Cocoapods了,但是过程很奇怪

当学习的人通常告诉你远离测试版时,当我在 Xcode 的稳定 AppStore-version 时,我 运行ning Big Sur B9;这可能是问题所在。 我完全卸载了 Xcode 12.0.1,然后从 Apple Developer 下载了 Xcode 12.2 beta。然后,我还下载并安装了 12.2 beta 的命令行工具,运行 再次在终端中安装了 cocoapods……瞧!

安装和 运行ning Xcode 测试版后,我能够安装并且现在工作正常。 也许当 BigSur 变为 public 时,这将得到解决?因为不是每个人都需要或必须 运行 Xcode beta 版本。

通过在 macOS Big Sur 上将 Xcode 升级到 12.2 来解决。

然后运行:

sudo gem install -n /usr/local/bin cocoapods

浪费了 5 个小时,因为我认为这是 gem 的问题。

我遇到过完全相同的问题

$ sudo gem install cocoapods
...
checking for ffi.h... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.
...

已通过将 ruby 版本升级到 2.7.2 来修复。

的学分

我使用 rbenv 来管理 ruby 个版本。

我遇到了同样的问题(Catalina 10.15.7 和 Xcode 12.3)。

我用了 homebrew 而不是 gem:

brew install cocoapods

XCode 的 ruby 框架似乎有问题。使用最新版本彻底重新安装 Xcode.app,运行ning xcode-select --install--select 对我不起作用。

这就是我使用 XCode 12.2 和 Catalina (10.15.7) 成功实现 运行 的方法:

TL;DR)

cd /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/ruby
ln -sf ../../../../Headers/ruby/config.h

cd /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0
ln -sf universal-darwin20 universal-darwin19

(修改符号链接中的错误拼写)

这对我有用:

https://github.com/CocoaPods/CocoaPods/issues/10286#issuecomment-747512270

引用:

  • Apple Developer Downloads
  • 下载命令行工具 11.5
  • sudo rm -rf /Library/Developer/CommandLineTools
  • 从 .dmg/.pkg 安装命令行工具
  • sudo xcode-select --switch /Library/Developer/CommandLineTools
  • [sudo] gem 安装 [--user-install] cocoapods
  • sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer

或者如果您已经安装了 cocoa pods 只需 运行

brew install cocoapods

在显示一些警告消息后

Warning: cocoapods 1.10.1 is already installed, it's just not linked.

对于link这个版本,运行: brew link cocoapods 或

brew link --overwrite cocoapods

这对我真的很有帮助。谢谢

以上溶胶。可能对每个人都适用,也可能不适用,但请确保交叉检查命令行工具。它必须设置且不能为空。

将默认 Xcode command-line 工具从版本 12.4 切换到 11.7 为我解决了这个问题。

我在 Catalina (10.15.7) 上安装了两个 Xcode 版本,所以我使用 Xcode 首选项(Locations/Command 线工具:)进行了切换,选择“Xcode 11.7" 在弹出菜单中。然后我再次退出 Xcode 和 运行 安装命令:

sudo gem install cocoapods

在我遇到同样的“缺少 header”错误之前,现在安装顺利进行了。

Command-line 工具可以单独安装,无需安装 Xcode 本身。然后你会使用终端来切换版本。当前默认工具的路径由

返回
xcode-select -p

并且切换版本是使用

完成的
sudo xcode-select -s {path-to-tools} 

安装 CocoaPods 后,我切换回最新的 command-line 工具,并对我当前的一个项目进行了 'pod install',该项目顺利成功。所以早期的版本似乎只需要安装 CocoaPods,而不是它的操作。

我一遍又一遍地遇到同样的错误,并尝试了多种方法来修复它,包括此处描述的许多方法,但都没有成功。

如此处解释:https://developer.apple.com/forums/thread/668456 ,根据某些方面,这可能对您有用,就像对我一样!

运行 sudo gem install -n /usr/local/bin cocoapods -v 1.8.4

显然最新的 cocoapads 版本,特别是最新的 (1.11.2) 将无法使用!

更新最新版本ruby: https://www.delftstack.com/howto/ruby/update-ruby-version-in-macos/ 并检查版本 ruby:

ruby -v

然后:

sudo gem install cocoapods