CentOS 7 一键安装 gcc 10

By Admin

centos
gcc
Linux

一键编译安装gcc ```bash #!/bin/bash # 一键编译安装gcc GCC_VER=10.4.0 MIN_GCC_VER=4 if [[ $EUID -ne 0 ]]; then echo "This script must be run as root or with sudo." exit 1 fi yum -y install wget gcc-c++ glibc-devel...

一键编译安装gcc

#!/bin/bash
# 一键编译安装gcc

GCC_VER=10.4.0
MIN_GCC_VER=4

if [[ $EUID -ne 0 ]]; then
  echo "This script must be run as root or with sudo." 
  exit 1
fi

yum -y install wget gcc-c++ glibc-devel libgcc bzip2 ccache 

# 检查当前GCC版本
CUR_GCC_VER=`gcc -dumpversion`
if [ ${CUR_GCC_VER%%.*} -lt ${MIN_GCC_VER} ]; then
    echo "Your GCC version ${CUR_GCC_VER} is too low. GCC ${GCC_VER} requires at least GCC ${MIN_GCC_VER}."
    exit 1 
fi

# 下载GCC ${GCC_VER}源码
echo "Downloading GCC ${GCC_VER} source code..."
wget http://ftp.gnu.org/gnu/gcc/gcc-${GCC_VER}/gcc-${GCC_VER}.tar.gz
if [ $? -ne 0 ]; then
    echo "Download failed! Please check your network connection."
    exit 1
fi

# 解压GCC源码
echo "Extracting GCC ${GCC_VER} source code..."
tar -zxf gcc-${GCC_VER}.tar.gz 
if [ $? -ne 0 ]; then
    echo "Extraction failed! Please check if you have permission to extract files."
    exit 1 
fi

# 下载编译依赖
echo "Downloading build prerequisites..."
cd gcc-${GCC_VER}  
./contrib/download_prerequisites 
if [ $? -ne 0 ]; then
    echo "Download failed! Please check your network connection."
    exit 1
fi

# 创建编译目录并配置
echo "Configuring build..."
mkdir gcc-build-${GCC_VER}
cd gcc-build-${GCC_VER}
../configure --prefix=/usr/local/gcc -enable-checking=release -enable-languages=c,c++ -disable-multilib
if [ $? -ne 0 ]; then
    echo "Configuration failed! Please check if all dependencies are met."
    exit 1
fi

# 编译安装
echo "Compiling and installing GCC ${GCC_VER}..." 
make -j $(nproc)
make install
if [ $? -ne 0 ]; then
    echo "Compilation failed! Please check if you have enough disk space and memory."
    exit 1 
fi

# 替换系统编译器
echo "Replacing system compilers..."
cd /usr/bin/  
mv cc cc.bak 
mv c++ c++.bak
mv /usr/bin/gcc /usr/bin/gcc-4.8.5
ln -sf /usr/local/gcc/bin/gcc cc 
ln -sf /usr/local/gcc/bin/gcc gcc
ln -sf /usr/local/gcc/bin/g++ c++
ln -sf /usr/local/gcc/bin/g++ g++
if [ $? -ne 0 ]; then
    echo "Failed to replace system compilers! Please check if you have permission."
    exit 1
fi

# 检查GCC版本  
gcc -v
g++ -v

# 替换libstdc++库链接
echo "Replacing libstdc++ links..."  
mv /usr/lib64/libstdc++.so.6 /usr/lib64/libstdc++.so.6.bak
ln -sf /usr/local/gcc/lib64/libstdc++.so.6.0.28 /usr/lib64/libstdc++.so.6 
ll /usr/lib64/libstdc++.so.6 
if [ $? -ne 0 ]; then
    echo "Failed to replace libstdc++ links! Please check if you have permission."
    exit 1
fi

echo "GCC ${GCC_VER} installed successfully!!!"