手动编译安装gcc

代码用到了一些C++17特性,但由于集群上的GCC版本比较老旧,出现了一些难以解决的段错误,因此选择了自己手动编译一份新版本GCC来避免此类问题。编译GCC虽然不难,但是整体较为繁琐,因此在此记录一下以免遗忘。

GCC的构建需要GMP(GNU Multiple Precision Arithmetic Library,GNU多精度算术库)、MPFR(Multiple Precision Floating-Point Reliable,多精度浮点可靠库)和MPC(Multiple Precision Complex,多精度复数库)这三个库。首先需要先按顺序安装这些库,并在配置GCC时指定它们的路径。

1.安装GMP

wget https://gmplib.org/download/gmp/gmp-6.2.1.tar.bz2
tar -xvf gmp-6.2.1.tar.bz2
cd gmp-6.2.1
./configure --prefix=$HOME/local/gmp-6.2.1
make
make install

2.安装MPFR

wget https://www.mpfr.org/mpfr-current/mpfr-4.2.0.tar.gz
tar -xvf mpfr-4.2.0.tar.gz
cd mpfr-4.2.0
./configure --prefix=$HOME/local/mpfr-4.2.0 --with-gmp-include=$HOME/local/gmp-6.2.1/include --with-gmp-lib=$HOME/local/gmp-6.2.1/lib64
make
make install

3.安装MPC

wget https://ftp.gnu.org/gnu/mpc/mpc-1.2.1.tar.gz
tar -xvf mpc-1.2.1.tar.gz
cd mpc-1.2.1
./configure --prefix=$HOME/local/mpc-1.2.1 --with-gmp-include=$HOME/local/gmp-6.2.1/include --with-gmp-lib=$HOME/local/gmp-6.2.1/lib64 --with-mpfr-include=$HOME/local/mpfr-4.2.0/include --with-mpfr-lib=$HOME/local/mpfr-4.2.0/lib64
make
make install

将GMP、MPFR和MPC的库文件路径添加到的LD_LIBRARY_PATH环境变量中。

export LD_LIBRARY_PATH=$HOME/local/gmp-6.2.1/lib64:$HOME/local/mpfr-4.2.0/lib64:$HOME/local/mpc-1.2.1/lib64:$LD_LIBRARY_PATH

4.安装gcc

wget https://ftp.gnu.org/gnu/gcc/gcc-9.3.0/gcc-9.3.0.tar.gz
tar -xvf gcc-9.3.0.tar.gz
cd gcc-9.3.0
./configure --prefix=$HOME/local/gcc-9.3.0 --with-gmp-include=$HOME/local/gmp-6.2.1/include --with-gmp-lib=$HOME/local/gmp-6.2.1/lib64 --with-mpfr-include=$HOME/local/mpfr-4.2.0/include --with-mpfr-lib=$HOME/local/mpfr-4.2.0/lib64 --with-mpc-include=$HOME/local/mpc-1.2.1/include --with-mpc-lib=$HOME/local/mpc-1.2.1/lib64 --disable-multilib
make
make install

加入–disable-multilib是因为这里没有32位开发库,否则会报错。

设置环境变量

export PATH=$HOME/local/gcc-9.3.0/bin:$PATH
export LIBRARY_PATH=$HOME/local/gcc-9.3.0/lib64:$LIBRARY_PATH
export LD_LIBRARY_PATH=$HOME/local/gcc-9.3.0/lib64:$LD_LIBRARY_PATH
export C_INCLUDE_PATH=$HOME/local/gcc-9.3.0/include:$C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH=$HOME/local/gcc-9.3.0/include:$CPLUS_INCLUDE_PATH

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据