LoginSignup
6
6

More than 5 years have passed since last update.

rbenv経由でRuby 2.1.4をビルドした際のエラーとその原因(OS X 10.9.5)

Last updated at Posted at 2014-11-06

Ruby 2.1.4のビルドエラー

OS X(10.9.5)上で、rbenvを使ってRuby 2.1.4をビルドした際に、以下のエラーが発生した。(後述の原因から察するに、rbenv経由でなく普通にRubyをビルドしようとした場合にも自身の環境では同様の問題が発生したと思われる)

$ rbenv install -v 2.1.4
...
In file included from dmydln.c:1:
./include/ruby/ruby.h:56:20: error: stdarg.h: No such file or directory
In file included from ./include/ruby.h:33,
                 from main.c:13:
./include/ruby/ruby.h:56:20: error: stdarg.h: No such file or directory
In file included from ./include/ruby/ruby.h:1694,
                 from ./include/ruby.h:33,
                 from main.c:13:
./include/ruby/intern.h:32:22: error: varargs.h: No such file or directory
In file included from ./include/ruby/ruby.h:1694,
                 from dmydln.c:1:
./include/ruby/intern.h:32:22: error: varargs.h: No such file or directory
compiling miniprelude.c
make: *** [dmydln.o] Error 1
make: *** Waiting for unfinished jobs....
make: *** [main.o] Error 1
In file included from miniinit.c:12:
./include/ruby/ruby.h:56:20: error: stdarg.h: No such file or directory
In file included from ./include/ruby/ruby.h:1694,
                 from miniinit.c:12:
./include/ruby/intern.h:32:22: error: varargs.h: No such file or directory
make: *** [miniinit.o] Error 1
In file included from miniprelude.c:6:
./include/ruby/ruby.h:56:20: error: stdarg.h: No such file or directory
In file included from ./include/ruby/ruby.h:1694,
                 from miniprelude.c:6:
./include/ruby/intern.h:32:22: error: varargs.h: No such file or directory
make: *** [miniprelude.o] Error 1

BUILD FAILED (OS X 10.9.5 using ruby-build 20141028-5-gd22a320)
...

stdarg.h/varargs.hがないといわれているが、インクルードパスを見ると、どうも含まれていそうに見える。

$ gcc -v test.c
...
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
...
$ ls /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/include | egrep '(stdarg|varargs)\.h'
stdarg.h
varargs.h

ビルドに使用するコンパイラのズレ

幸い同じ手順でビルドできた別環境があったので、そちらでのビルド時のログと見比べてみる。すると、ビルドできた環境ではコンパイラにclangが使われており、ビルドに失敗した環境ではgcc-4.2が使われていることに気付いた。

コンパイラの違い
# ビルドできた環境のログ
...
config.status: creating ruby-2.1.pc
    CC = clang
    LD = ld
    LDSHARED = clang -dynamic -bundle
...

# ビルドできなかった環境のログ
...
config.status: creating ruby-2.1.pc
    CC = gcc-4.2
    LD = ld
    LDSHARED = gcc-4.2 -dynamic -bundle
...

すこし遡ってconfigure時のログを見ると、gcc-4.2がある場合はそちらが優先して使用することが分かる。

# ビルドできた環境のログ
...
Installing ruby-2.1.4...
checking build system type... x86_64-apple-darwin13.4.0
checking host system type... x86_64-apple-darwin13.4.0
checking target system type... x86_64-apple-darwin13.4.0
checking for gcc-4.2... no
checking for clang... clang
checking for gcc... (cached) clang
...

# ビルドできなかった環境のログ
...
Installing ruby-2.1.4...
checking build system type... x86_64-apple-darwin13.4.0
checking host system type... x86_64-apple-darwin13.4.0
checking target system type... x86_64-apple-darwin13.4.0
checking for gcc-4.2... gcc-4.2
checking for gcc... (cached) gcc-4.2
...

ビルドできなかった環境はTime Machine経由で昔から継いできたため、gcc-4.2が存在していたと思われる(裏付けは取れてないけれど)。
gcc-4.2のインクルードパスを調べたところ、確かにstdarg.h/varargs.hを含むディレクトリが入っていなかった。

$ /usr/bin/gcc-4.2 -v test.c
...
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
...
# stdarg.h/varargs.hは /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/include下にある。

解決方法

あまりスマートな解決方法ではないがビルド前にgcc-4.2をリネームすることで、clangを使わせることができた。これにより、Rubyのビルドは成功した。

$ sudo mv /usr/bin/gcc-4.2 /usr/bin/gcc-4.2_org
$ rbenv install -v 2.1.4
...
Installing ruby-2.1.4...
checking build system type... x86_64-apple-darwin13.4.0
checking host system type... x86_64-apple-darwin13.4.0
checking target system type... x86_64-apple-darwin13.4.0
checking for gcc-4.2... no
checking for clang... clang
checking for gcc... (cached) clang
...
config.status: creating ruby-2.1.pc
    CC = clang
    LD = ld
    LDSHARED = clang -dynamic -bundle
...


$ rbenv versions
* system (set by /Users/tatsuro/.anyenv/envs/rbenv/version)
  2.1.4

追記(2014/11/07)

環境変数CCをrbenv installを使う前に指定することで使用するコンパイラを指定できることが分かった。あとで問題となった環境で試す。->問題なくインストールできることを確認。

$ export CC=clang; rbenv install -v 2.1.4; unset CC
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6