RMagickのGemインストールが通らないときは

Windows 7で、Ruby2.0にRMagick 2.13.2をインストールしてずいぶんはまった。

Ruby2.0はRubyInstallerの32ビット版+DevKit、ImageMagickは6.8.7-Q8で、やはり32ビット版。

ここでふつうにgem installすると、stdint.hのチェックでとまる。

>gem i rmagick
Fetching: rmagick-2.13.2.gem (100%)
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
ERROR:  Error installing rmagick:
        ERROR: Failed to build gem native extension.

    C:/Ruby200/bin/ruby.exe extconf.rb
checking for Ruby version >= 1.8.5... yes
checking for stdint.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.

このメッセージを見るとstdint.hがないということなので、DevKitをインストールしなおしたり、ずいぶん遠回りしたが、本当に重要なメッセージはこれだった。

Gem files will remain installed in C:/Ruby200/lib/ruby/gems/2.0.0/gems/rmagick-2.13.2 for inspection.
Results logged to C:/Ruby200/lib/ruby/gems/2.0.0/gems/rmagick-2.13.2/ext/RMagick/gem_make.out

コンパイルのログが作業ディレクトリのgem_make.outにあるよと。
するとなんのことはない、ImageMagickのライブラリが見つからないというエラーだった。

stdint.hの有無を確認するために、stdint.hをインクルードしただけのソースをコンパイルして、通るかどうかを見ている。
ソースのコンパイルはできているのに、リンクの段で詰まっていたのだ。

実は rmagick stdint.h とかでぐぐって、この解決方法自体は目にしていたのに、「stdint.hの問題だから--with-opt-dir関係ないだろ」とか思って無視していた。大失敗。

そこで、コマンドラインImageMagickディレクトリを明示する。

>gem install rmagick -- --with-opt-dir=c:\ImageMagick-6.8.7-Q8

順調にコンパイルが進むが最後の最後でエラーが出た。

linking shared-object RMagick2.so
rmimage.o: In function `call_trace_proc':
C:\Ruby200\lib\ruby\gems\2.0.0\gems\rmagick-2.13.2\ext\RMagick/rmimage.c:15053: undefined reference to `rb_frame_last_func'
rmutil.o: In function `rm_not_implemented':
C:\Ruby200\lib\ruby\gems\2.0.0\gems\rmagick-2.13.2\ext\RMagick/rmutil.c:771: undefined reference to `rb_frame_last_func'
rmutil.o: In function `rm_progress_monitor':
C:\Ruby200\lib\ruby\gems\2.0.0\gems\rmagick-2.13.2\ext\RMagick/rmutil.c:1442: undefined reference to `rb_frame_last_func'
collect2.exe: error: ld returned 1 exit status
make: *** [RMagick2.so] Error 1

これは title:http://stackoverflow.com/questions/4989097/rmagick-under-windows を参考に、gemのオプションで解決できた。

>gem ins rmagick --platform=ruby --with-opt-dir=c:\ImageMagick-6.8.7-Q8
...
Successfully installed rmagick-2.13.2
Parsing documentation for rmagick-2.13.2
unable to convert "\x90" from ASCII-8BIT to UTF-8 for ext/RMagick/RMagick2.so, skipping
unable to convert "\xF8" from ASCII-8BIT to UTF-8 for ext/RMagick/rmagick.o, skipping
...
unable to convert "\xC1" from ASCII-8BIT to UTF-8 for ext/RMagick/rmutil.o, skip ping
unable to convert "\x90" from ASCII-8BIT to UTF-8 for lib/RMagick2.so, skipping
Installing ri documentation for rmagick-2.13.2
1 gem installed

ドキュメントのパースでエラーがいっぱい出たがバイナリのインストールには成功している。