1.更新操作系统自带的gcc g++
# base redhat8.10
yum update
yum install -y gcc gcc-c++
yum install -y bzip2
2.下载gcc和依赖
wget -P /data/base https://mirrors.aliyun.com/gnu/gcc/gcc-10.5.0/gcc-10.5.0.tar.gz
cd /data/base && tar -xzvf gcc-10.5.0.tar.gz
cd /data/base/gcc-10.5.0
./contrib/download_prerequisites
若服务器无法上网,可以从https://mirrors.aliyun.com/gnu/gcc/gcc-10.2.0/gcc-10.2.0.tar.gz下载到本地
解压后查看/contrib/download_prerequisites ,根据里面的base_url=http://gcc.gnu.org/pub/gcc/infrastructure/下载
gmp-6.1.0.tar.bz2、mpfr-3.1.6.tar.bz2、mpc-1.0.3.tar.gz、isl-0.18.tar.bz2到/data/base/gcc-10.2.0 目录下,然后再执行./contrib/download_prerequisites(需要注释以下代码,下载文件的shell)
#定义下载的依赖包
gmp='gmp-6.1.0.tar.bz2'
mpfr='mpfr-3.1.6.tar.bz2'
mpc='mpc-1.0.3.tar.gz'
isl='isl-0.18.tar.bz2'# 注释下载依赖包的shell脚本
220 #for ar in $(echo_archives)
221 #do
222 # if [ ${force} -gt 0 ]; then rm -f "${directory}/${ar}"; fi
223 # [ -e "${directory}/${ar}" ] \
224 # || ${fetch} --no-verbose -O "${directory}/${ar}" "${base_url}${ar}" \
225 # || die "Cannot download ${ar} from ${base_url}"
226 #done
227 #unset ar
3.编译并安装
mkdir /usr/lib/gcc/x86_64-redhat-linux/10.5.0
mkdir /data/base/gcc-build-10.5.0
cd /data/base/gcc-build-10.5.0
../gcc-10.5.0/configure --prefix=/usr/lib/gcc/x86_64-redhat-linux/10.5.0/ --enable-checking=release --enable-languages=c,c++ --disable-multilib
# 编译(使用多核加速)
make -j$(nproc) && make install
编译并安装好之后好之后返回如下:
----------------------------------------------------------------------
Libraries have been installed in:/usr/lib/gcc/x86_64-redhat-linux/10.1.0/lib/../lib64If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:- add LIBDIR to the `LD_LIBRARY_PATH' environment variableduring execution- add LIBDIR to the `LD_RUN_PATH' environment variableduring linking- use the `-Wl,-rpath -Wl,LIBDIR' linker flag- have your system administrator add LIBDIR to `/etc/ld.so.conf'See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
make[4]: 对“install-data-am”无需做任何事。
make[4]: 离开目录“/opt/gcc-build-10.1.0/x86_64-pc-linux-gnu/libatomic”
make[3]: 离开目录“/opt/gcc-build-10.1.0/x86_64-pc-linux-gnu/libatomic”
make[2]: 离开目录“/opt/gcc-build-10.1.0/x86_64-pc-linux-gnu/libatomic”
make[1]: 离开目录“/opt/gcc-build-10.1.0”
4.初始化gcc配置
系统之前安装了8.5.0版本,可以使用alternatives配置管理gcc
mv /usr/bin/gcc /usr/bin/gcc-8.5.0
mv /usr/bin/g++ /usr/bin/g++-8.5.0
alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8.5.0 88 --slave /usr/bin/g++ g++ /usr/bin/g++-8.5.0
alternatives --install /usr/bin/gcc gcc /usr/lib/gcc/x86_64-redhat-linux/10.5.0/bin/x86_64-pc-linux-gnu-gcc 99 --slave /usr/bin/g++ g++ /usr/lib/gcc/x86_64-redhat-linux/10.5.0/bin/x86_64-pc-linux-gnu-g++
alternatives --config gcc共有 2 个提供“gcc”的程序。选项 命令
-----------------------------------------------1 /usr/bin/gcc-8.5.0
*+ 2 /usr/lib/gcc/x86_64-redhat-linux/10.5.0/bin/x86_64-pc-linux-gnu-gcc按 Enter 保留当前选项[+],或者键入选项编号: