当前位置:网站首页>One click GCC script installation

One click GCC script installation

2022-06-26 15:09:00 BOGO

GCC yes GUN Compiler Collection For short , In addition to the compiler , It also contains other related tools , It can build the source code written in high-level language that is easy for human to use into binary code that can be directly executed by computer .

GCC yes Linux The most commonly used compiler under the platform , It is Linux The de facto standard for platform compilers . meanwhile , stay Linux Embedded development field under the platform ,GCC It is also the most commonly used compiler .GCC It is widely used , Because it can support different target architectures . for example , It supports both host based development ( Simply put, it means compiling programs for a certain platform , Compile on this platform ), Cross compiling is also supported ( That is to say A The program compiled on the platform is for the platform B The use of ). at present ,GCC There are more than forty supported architectures , Common are X86 series 、Arm、PowerPC etc. . meanwhile ,GCC It can also run on different operating systems , Such as Linux、Solaris、Windows etc. .

In addition to the above ,GCC In addition to supporting C Language , Many other languages are also supported , for example C++、Ada、Java、Objective-C、FORTRAN、Pascal、go etc. .

Basically, many applications need to use... When compiling and installing gcc, Generally, the version used is not very high, so you can use the shortcut installation command :

yum install -y gcc-c++

But some applications need a higher version when compiling gcc, At this time, you can only compile and install , and gcc Compiling and installing is very troublesome , Several dependent components of the specified version are required to compile and install successfully , For the convenience of installation, an installation shell Script , Unattended installation through scripts , Of course, the installation and compilation process is still a bit long , The script only supports CentOS System ( System version 5~7 Have you tested ) To install ( Other systems need to adjust the command of quickly installing components yum And the corresponding installation package name )

#!/bin/bash
#   Official website  https://gcc.gnu.org/
#
#  Download address  ( Mirror image )
# https://gcc.gnu.org/mirrors.html
#
#  Dependency statement 
# https://gcc.gnu.org/install/prerequisites.html
#
#   Command parameter 
#  $1  Specify the installation version , If not, get the latest version number , by  new  Install the latest version when 
#

#GCC version number
GCC_VERSION=$1
# GCC Installation base directory 
INSTALL_BASE="/usr/local/gcc/"
#  Required library base directory 
INSTALL_PACKAGE_BASE="/usr/local/"
# gcc  Compile configuration 
GCC_CONFIGURE_WITH=''
#  Mirror address 
MIRRORS_URL="http://mirror.linux-ia64.org/gnu/gcc"

if [ -z $1 ] || [[ $1 == "new" ]]; then
    echo "gcc version is empty!"
    echo " Get the latest stable version number  ...";
    GCC_VERSION=`curl $MIRRORS_URL/releases/ 2>&1| grep -P 'gcc-\d+\.\d+\.\d+' -o|tail -n 1|grep -P '\d+\.\d+\.\d+' -o`
    if [ -z "$GCC_VERSION" ];then
        echo " Failed to get version !";
        exit
    fi
    if [ -z $1 ]; then
        echo $GCC_VERSION
        exit
    fi
fi
if [ -e "$INSTALL_BASE$GCC_VERSION/bin/gcc" ];then
    echo "gcc-$GCC_VERSION already install!"
    exit
fi

OLD_PATH=`pwd`
if [[ "$0" =~ '/' ]]; then
    cd "`echo "$0" | grep -P '(/?[^/]+/)+' -o`"
    CURRENT_PATH=`pwd`
    cd $OLD_PATH
else
    CURRENT_PATH=$OLD_PATH
fi

if [ ! -d "gcc" ];then
    mkdir gcc
fi
cd gcc
if [ ! -e "gcc-$GCC_VERSION.tar.gz" ]; then
    #download php
    echo "download gcc-$GCC_VERSION.tar.gz";
    wget $MIRRORS_URL/releases/gcc-$GCC_VERSION/gcc-$GCC_VERSION.tar.gz 2>&1
fi
if [ ! -d "gcc-$GCC_VERSION" ]; then
    #decompression
    echo "decompression gcc-$GCC_VERSION.tar.gz";
    tar -zxf gcc-$GCC_VERSION.tar.gz
fi
if [ ! -d "gcc-$GCC_VERSION" ]; then
    echo "gcc-$GCC_VERSION dir is not exists"
    exit
fi

echo "install dependence"

yum install -y gcc-c++ bzip2 ntpdate m4
#  Time out of sync is easy to cause configuration dead cycle 
ntpdate -u ntp.api.bz

# while  What is recycled is pipes , Will start the subprocess , Cannot modify external variables 

PACKAGE_LISTS=`cat gcc-$GCC_VERSION/contrib/download_prerequisites| grep -P '\-\d+\.\d+(\.\d+)?\.tar'`
for LINE in `echo -e $PACKAGE_LISTS`
do
    PACKAGE_VERSION_FILE=`echo $LINE|grep -P '\w+\-\d+\.\d+(\.\d+)?\.tar\.(bz2|gz)' -o`
    PACKAGE=`echo $PACKAGE_VERSION_FILE|grep -P '^\w+' -o`
    PACKAGE_VERSION_DIR=`echo $PACKAGE_VERSION_FILE|grep -P '\w+\-\d+\.\d+(\.\d+)?' -o`
    PACKAGE_VERSION=`echo $PACKAGE_VERSION_DIR|grep -P '\d+\.\d+(\.\d+)?' -o`
    PACKAGE_CONFIGURE_WITH=$GCC_CONFIGURE_WITH
    GCC_CONFIGURE_WITH="$PACKAGE_CONFIGURE_WITH --with-$PACKAGE=$INSTALL_PACKAGE_BASE$PACKAGE/$PACKAGE_VERSION"
    echo "install $PACKAGE_VERSION_DIR"
    if [ -d "$INSTALL_PACKAGE_BASE$PACKAGE/$PACKAGE_VERSION" ]; then
        echo "$PACKAGE_VERSION_DIR already install";
        continue;
    fi
    if [ ! -e "$PACKAGE_VERSION_FILE" ]; then
        #download package
        echo "download $PACKAGE_VERSION_FILE";
        wget $MIRRORS_URL/infrastructure/$PACKAGE_VERSION_FILE 2>&1
    fi
    if [ ! -d "$PACKAGE_VERSION_DIR" ]; then
        #decompression
        echo "decompression $PACKAGE_VERSION_FILE";
        if [ -n "`echo $PACKAGE_VERSION_FILE|grep -P '\.gz$'`" ];then
            tar -zxf $PACKAGE_VERSION_FILE
        else
            tar -xf $PACKAGE_VERSION_FILE
        fi
    fi
    if [ ! -d "$PACKAGE_VERSION_DIR" ]; then
        echo "$PACKAGE_VERSION_DIR dir is not exists"
        exit
    fi
    cd $PACKAGE_VERSION_DIR
    if [[ "$PACKAGE" == "isl" ]];then
        PACKAGE_CONFIGURE_WITH=' --with-gmp-prefix='`echo $PACKAGE_CONFIGURE_WITH|grep -P "[^=]+gmp/\d+\.\d+\.\d+" -o`
    fi
    echo "./configure --prefix=$INSTALL_PACKAGE_BASE$PACKAGE/$PACKAGE_VERSION$PACKAGE_CONFIGURE_WITH"
    ./configure --prefix=$INSTALL_PACKAGE_BASE$PACKAGE/$PACKAGE_VERSION$PACKAGE_CONFIGURE_WITH 2>&1
    make 2>&1
    make install 2>&1
    if [ ! -d "$INSTALL_PACKAGE_BASE$PACKAGE/$PACKAGE_VERSION" ] || [ -z "`ls $INSTALL_PACKAGE_BASE$PACKAGE/$PACKAGE_VERSION`" ];then
        echo "$PACKAGE-$PACKAGE_VERSION install fail!"
        exit
    fi
    if [[ "$PACKAGE" == "isl" ]];then
        echo "mv lib/*.py file"
        #  eliminate py file , These files will affect the shared dynamic link library ldconfig Command execution failed 
        for PY_FILE in `find $INSTALL_PACKAGE_BASE$PACKAGE/$PACKAGE_VERSION/lib/ -name "*.py"`
        do
            if [ -n "$PY_FILE" ] && [ -e "$PY_FILE" ];then
                echo "mv $PY_FILE $INSTALL_PACKAGE_BASE$PACKAGE/$PACKAGE_VERSION"
                mv $PY_FILE $INSTALL_PACKAGE_BASE$PACKAGE/$PACKAGE_VERSION
            fi
        done
    fi
    #  Shared dynamic link library , Load the configuration 
    if [ -d "$INSTALL_PACKAGE_BASE$PACKAGE/$PACKAGE_VERSION/lib" ] && [ -z "`cat /etc/ld.so.conf|grep "$INSTALL_PACKAGE_BASE$PACKAGE/$PACKAGE_VERSION"`" ];then
        echo "$INSTALL_PACKAGE_BASE$PACKAGE/$PACKAGE_VERSION/lib" >> /etc/ld.so.conf
        ldconfig
    fi
    cd ../
done

cd gcc-$GCC_VERSION
echo "install gcc"
make clean 2>&1
# 64 Bit system needs to be disabled multilib
if [ -n "`uname -a|grep -P 'el\d+\.x\d+_\d+' -o|grep x86_64 -o`" ]; then
    GCC_CONFIGURE_WITH=$GCC_CONFIGURE_WITH' --disable-multilib'
fi

#  The new version needs to download the configuration file 
if [ ! -e "./configure" ] && [ -e "./contrib/download_prerequisites" ];then
    ./contrib/download_prerequisites
     mkdir gcc-make-tmp
     cd gcc-make-tmp
    echo "../configure --prefix=$INSTALL_BASE$GCC_VERSION$GCC_CONFIGURE_WITH"
    ../configure --prefix=$INSTALL_BASE$GCC_VERSION$GCC_CONFIGURE_WITH 2>&1
else
    echo "./configure --prefix=$INSTALL_BASE$GCC_VERSION$GCC_CONFIGURE_WITH"
    ./configure --prefix=$INSTALL_BASE$GCC_VERSION$GCC_CONFIGURE_WITH 2>&1
fi

HTREAD_NUM=`lscpu |grep Thread|grep -P '\d+$' -o`
make -j $HTREAD_NUM 2>&1
make install 2>&1

# check install status
if [ ! -d "$INSTALL_BASE$GCC_VERSION" ] || [ -z "`ls $INSTALL_BASE$GCC_VERSION`" ]; then
    echo "[error] install gcc-$GCC_VERSION fail!";
else
    echo "$INSTALL_BASE$GCC_VERSION/lib64" >> /etc/ld.so.conf
    echo "mv lib64/*.py file"
    #  eliminate py file , These files will affect the shared dynamic link library ldconfig Command execution failed 
    for PY_FILE in `find $INSTALL_BASE$GCC_VERSION/lib64/ -name "*.py"`
    do
        if [ -n "$PY_FILE" ] && [ -e "$PY_FILE" ];then
            echo "mv $PY_FILE $INSTALL_BASE$GCC_VERSION"
            mv $PY_FILE $INSTALL_BASE$GCC_VERSION
        fi
    done
    ldconfig
    echo 'export PATH=$PATH:'"$INSTALL_BASE$GCC_VERSION/bin" >> /etc/profile
    source /etc/profile
    yum remove -y gcc-c++
    echo "install gcc-$GCC_VERSION success!";
fi

Usage method :

stay root Under Account No , Create a .sh Suffix shell Script files , such as :gcc-install.sh Write and save the above installation code .

Execute the installation command

bash gcc-install.sh new

If the installation is unsuccessful due to too few system dependencies , You need to add the corresponding dependent packages ( Here, we only take the test system as an example. The dependent packages may not be installed enough ), The installation directory can be configured and modified in the installation script , After the installation is successful, you will be prompted that the installation is successful .

原网站

版权声明
本文为[BOGO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261451205277.html