首页 > 解决方案 > Rmagick - Gem::Ext::BuildError:错误:无法构建 gem 原生扩展

问题描述

我安装pkg-configimagemagick

brew install pkg-config imagemagick@6

Imagemagick 版本

identify -version
Version: ImageMagick 6.9.12-28 Q16 arm 2021-10-29 https://imagemagick.org

尝试安装 rmagick 时

gem install rmagick

我明白了

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

    current directory: /opt/homebrew/lib/ruby/gems/3.0.0/gems/rmagick-4.2.3/ext/RMagick
/opt/homebrew/opt/ruby/bin/ruby -I /opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0 -r ./siteconf20211112-33588-cd2c64.rb extconf.rb
checking for brew... yes
checking for Ruby version >= 2.3.0... yes
checking for pkg-config... yes
/opt/homebrew/Library/Homebrew/shims/mac/super/pkg-config: line 9: /pkg-config/bin/pkg-config: No such file or directory
/opt/homebrew/Library/Homebrew/shims/mac/super/pkg-config: line 9: exec: /pkg-config/bin/pkg-config: cannot execute: No such file or directory


ERROR: Can't install RMagick 4.2.3.
Can't find the ImageMagick library or one of the dependent libraries.
Check the mkmf.log file for more detailed information.


*** 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
    --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=/opt/homebrew/Cellar/ruby/3.0.2_1/bin/$(RUBY_BASE_NAME)

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

  /opt/homebrew/lib/ruby/gems/3.0.0/extensions/arm64-darwin-20/3.0.0/rmagick-4.2.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /opt/homebrew/lib/ruby/gems/3.0.0/gems/rmagick-4.2.3 for inspection.
Results logged to /opt/homebrew/lib/ruby/gems/3.0.0/extensions/arm64-darwin-20/3.0.0/rmagick-4.2.3/gem_make.out

所以我创建了一个/opt/homebrew/Library/Homebrew/shims/mac/super/pkg-config/bin/包含符号链接的文件夹

ln -s /opt/homebrew/opt/pkg-config/bin/pkg-config pkg-config

现在,当尝试安装 rmagick 时,我得到:

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

    current directory: /opt/homebrew/lib/ruby/gems/3.0.0/gems/rmagick-4.2.3/ext/RMagick
/opt/homebrew/opt/ruby/bin/ruby -I /opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0 -r ./siteconf20211112-33523-27c8up.rb extconf.rb
checking for brew... yes
checking for Ruby version >= 2.3.0... yes
checking for pkg-config... yes
checking for outdated ImageMagick version (<= 6.7.7)... no
checking for clang... yes

Warning: Found more than one ImageMagick installation. This could cause problems at runtime.
         /opt/homebrew/opt/imagemagick@6/bin/Magick-config reports version 6.9.12-28 Q16 is installed in /opt/homebrew/Cellar/imagemagick@6/6.9.12-28
         /opt/homebrew/bin/Magick-config reports version 6.9.12-28 Q16 is installed in /opt/homebrew/Cellar/imagemagick@6/6.9.12-28
Using 6.9.12-28 Q16 from /opt/homebrew/Cellar/imagemagick@6/6.9.12-28.

checking for __GNUC__... *** 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
    --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=/opt/homebrew/Cellar/ruby/3.0.2_1/bin/$(RUBY_BASE_NAME)
/opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/mkmf.rb:471:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
    from /opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/mkmf.rb:597:in `block in try_compile'
    from /opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/mkmf.rb:546:in `with_werror'
    from /opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/mkmf.rb:597:in `try_compile'
    from /opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/mkmf.rb:864:in `macro_defined?'
    from /opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/mkmf.rb:1006:in `block in have_macro'
    from /opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/mkmf.rb:971:in `block in checking_for'
    from /opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/mkmf.rb:361:in `block (2 levels) in postpone'
    from /opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/mkmf.rb:331:in `open'
    from /opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/mkmf.rb:361:in `block in postpone'
    from /opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/mkmf.rb:331:in `open'
    from /opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/mkmf.rb:357:in `postpone'
    from /opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/mkmf.rb:970:in `checking_for'
    from /opt/homebrew/Cellar/ruby/3.0.2_1/lib/ruby/3.0.0/mkmf.rb:1005:in `have_macro'
    from extconf.rb:99:in `configure_compile_options'
    from extconf.rb:19:in `initialize'
    from extconf.rb:398:in `new'
    from extconf.rb:398:in `<main>'

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

  /opt/homebrew/lib/ruby/gems/3.0.0/extensions/arm64-darwin-20/3.0.0/rmagick-4.2.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /opt/homebrew/lib/ruby/gems/3.0.0/gems/rmagick-4.2.3 for inspection.
Results logged to /opt/homebrew/lib/ruby/gems/3.0.0/extensions/arm64-darwin-20/3.0.0/rmagick-4.2.3/gem_make.out

指定我必须安装development tools,所以我检查了它是否已安装:

gcc -v

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.5 (clang-1205.0.22.11)
Target: arm64-apple-darwin20.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

我已经搜索了很长时间,发现了几个处理类似问题的主题,但没有一个可以帮助我......

你知道为什么这不起作用吗?

我正在使用Mac OSX 11.6ruby 3.0.2p107

非常感谢

标签: rubyrubygemsimagemagickrmagickpkg-config

解决方案


好的,只需重新启动mac,卸载并重新安装我设法安装的所有内容rmagick v4.2.3

卸载

brew uninstall pkg-config imagemagick@6

重新安装

brew install pkg-config imagemagick@6

并安装了 Rmagick

gem install rmagick

我之前已经尝试卸载并重新安装,但没有重新启动mac,并且Rmagick安装以同样的方式失败......所以似乎需要重新启动,也许其中一个路径或环境变量需要为Rmagick正确设置...


推荐阅读