gcc 7.3.0をソースからコンパイル&インストール


ホームディレクトリでビルドしたり、展開したソースディレクトリとビルドディレクトリを分けなかったりするとうまくビルドできないことが多かったので、これでやれば確実にビルドできるという方法を書き残しておきます。

環境は以下です。

  • Amazon Linux
  • sudo yum groupinstall "Development Tools" 実行済み

手順

gcc公式のインストール手順を参考に行う。

sudo su -
cd /usr/local/src
wget http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gcc-7.3.0/gcc-7.3.0.tar.gz
tar zxvf gcc-7.3.0.tar.gz
cd gcc-7.3.0
./contrib/download_prerequisites
mkdir build
cd build
../configure --enable-languages=c,c++ --prefix=/usr/local --disable-bootstrap --disable-multilib
make -j`nproc` # CPUコア数に応じて並列コンパイル
make install

make install で ldconfig のエラーが出たら手動で修正

ldconfig: /usr/local/lib/../lib64/libstdc++.so.6.0.24-gdb.py is not an ELF file - it has the wrong magic bytes at the start.

のようなエラーが出たら、対象のファイルを削除して再度ldconfigを実行する。

mv /usr/local/lib/../lib64/libstdc++.so.6.0.24-gdb.py /usr/local/lib/../lib64/ignore-libstdc++.so.6.0.24-gdb.py
ldconfig

バージョン確認

gcc --version

rootユーザでよくあるが、 /usr/loca/bin にパスが通ってないと新しい方のgccを参照できないので注意。

注意点

ビルド用のディレクトリ objdir をソースディレクトリ srcdir とは別に作る

First, we highly recommend that GCC be built into a separate directory from the sources which does not reside within the source tree. This is how we generally build GCC; building where srcdir == objdir should still work, but doesn’t get extensive testing; building where objdir is a subdirectory of srcdir is unsupported.

https://gcc.gnu.org/install/configure.html から引用

configureのprefixオプションはshellが認識できる場所を指定する

We highly recommend against dirname being the same or a subdirectory of objdir or vice versa. If specifying a directory beneath a user’s home directory tree, some shells will not expand dirname correctly if it contains the ‘~’ metacharacter; use $HOME instead.

https://gcc.gnu.org/install/configure.html から引用