onsen code monkey

個人的な日記とプログラミング備忘録です

gcc: error: unrecognized command line option を解決する

rails で bcrypt をインストールしようとすると以下のようなエラーが出た

Installing bcrypt 3.1.19 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory: /home/vagrant/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bcrypt-3.1.19/ext/mri
/home/vagrant/.rbenv/versions/3.2.2/bin/ruby extconf.rb
creating Makefile

current directory: /home/vagrant/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bcrypt-3.1.19/ext/mri
make DESTDIR\= sitearchdir\=./.gem.20231014-3779-vcohcj sitelibdir\=./.gem.20231014-3779-vcohcj clean

current directory: /home/vagrant/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bcrypt-3.1.19/ext/mri
make DESTDIR\= sitearchdir\=./.gem.20231014-3779-vcohcj sitelibdir\=./.gem.20231014-3779-vcohcj
compiling bcrypt_ext.c
gcc: error: unrecognized command line option ‘-Wduplicated-cond’
gcc: error: unrecognized command line option ‘-Wmisleading-indentation’
gcc: error: unrecognized command line option ‘-Wimplicit-fallthrough=0’
make: *** [bcrypt_ext.o] Error 1

make failed, exit code 2

~ 中略

An error occurred while installing bcrypt (3.1.19), and Bundler cannot continue.

In Gemfile:
bcrypt

どうやら gcc のバージョンが古いせいでコンパイラコマンドラインオプションを認識できないため bcrypt のインストールに失敗している模様

gcc のバージョンを確認してみる

$ gcc -v

結果

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)

version 4.8.5 とのこと。2015年のバージョンらしいのでたしかにかなり古そう。
以下、アップデート手順(CentOS/Red Hat用のコマンドのため他を使用している場合は読み替えてください)

gcc アップデートコマンド

$ sudo yum update gcc

これでアップデートできればそれで良し。
僕の場合はさらに以下のエラーメッセージが出ました。

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.jaist.ac.jp
* centos-sclo-rh: ftp.jaist.ac.jp
* centos-sclo-sclo: ftp.jaist.ac.jp
* extras: ftp.jaist.ac.jp
* updates: mirrors.163.com
No packages marked for update

よくわからないがとにかくダメそうなので別の方法を調べてみると、どうやら Developer Toolset の新しいバージョンをインストールすれば良いとのことで Developer Toolset 9 をインストールする

$ sudo yum install centos-release-scl
$ sudo yum install devtoolset-9

Developer Toolset 9 を使用するように設定

$ source scl_source enable devtoolset-9

改めて gcc のバージョンを確認

$ gcc -v

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-9/root/usr --mandir=/opt/rh/devtoolset-9/root/usr/share/man --infodir=/opt/rh/devtoolset-9/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-9.3.1-20200408/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC)

9.3.1 にアップデートできたので、あとは bundle install で bcrypt をインストールすれば無事成功