有些时候,实验或者项目需要特定的gcc(g++)版本,但是并没有必要将全部的环境都变为该特定的版本,且网上很多的gcc的更新教程都需要root权限,本文旨在给出非root权限下的gcc安装。

1 通过链接下载不同版本的gcc(需要的版本)

网页截图
在这里插入图片描述

2 解压下载的压缩包(这个压缩包其实是个安装包)
tar -xjvf gcc-5.3.0.tar.bz2   # 解压 tar.bz2
---------或者--------
tar zxvf gcc-5.3.0.tar.bz   #解压tar.bz
3 进入解压后的文件夹,执行以下命令
[home gcc-5.3.0] ./contrib/download_prerequisites  # 执行下载gcc依赖包命令(需要科学联网下载)
4 上步执行完后用 ls 命令可以发现以下的文件列表

文件列表图

5 用以下命令,查看并拷贝建立软链接的代码
[home gcc-5.3.0] cat contrib/download_prerequisites
你会看到
#! /bin/sh

# Download some prerequisites needed by gcc.
# Run this from the top level of the gcc source tree and the gcc
# build will do the right thing.
#
# (C) 2010 Free Software Foundation
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.

# If you want to disable Graphite loop optimizations while building GCC,
# DO NOT set GRAPHITE_LOOP_OPT as yes so that the ISL package will not
# be downloaded.
GRAPHITE_LOOP_OPT=yes

if [ ! -e gcc/BASE-VER ] ; then
	echo "You must run this script in the top level GCC source directory."
	exit 1
fi

# Necessary to build GCC.
MPFR=mpfr-2.4.2
GMP=gmp-4.3.2
MPC=mpc-0.8.1

wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$MPFR.tar.bz2 || exit 1
tar xjf $MPFR.tar.bz2 || exit 1
ln -sf $MPFR mpfr || exit 1

wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$GMP.tar.bz2 || exit 1
tar xjf $GMP.tar.bz2  || exit 1
ln -sf $GMP gmp || exit 1

wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$MPC.tar.gz || exit 1
tar xzf $MPC.tar.gz || exit 1
ln -sf $MPC mpc || exit 1

# Necessary to build GCC with the Graphite loop optimizations.
if [ "$GRAPHITE_LOOP_OPT" = "yes" ] ; then
  ISL=isl-0.14

  wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$ISL.tar.bz2 || exit 1
  tar xjf $ISL.tar.bz2  || exit 1
  ln -sf $ISL isl || exit 1
fi

6 编译GCC(用系统的GCC编译自己的GCC){依旧在GCC安装包的根目录}
[home gcc-5.3.0] mkdir objdir
[home gcc-5.3.0] cd objdir
[home gcc-5.3.0] ../configure --disable-checking --enable-languages=c,c++ --disable-multilib --prefix=/path/to/install/gcc-5.3 --enable-threads=posix
[home gcc-5.3.0] make -j64    # 多线程编译,否则很慢很慢很慢,能多开就多开几个线程
[home gcc-5.3.0] make install
【注:/path/to/install/gcc-5.3 为我们install后存的gcc的目录】
7 为当前用户配置环境变量
7.1 切换到当前用户的目录,然后运行命令
[home user1] vi ~/.bashrc # 打开~/.bashrc文件
7.2 在末尾加入下面的代码
export PATH=/path/to/install/gcc-5.3/bin:/path/to/install/gcc-5.3/lib64:$PATH
export LD_LIBRARY_PATH=/path/to/install/gcc-5.3/lib/:$LD_LIBRARY_PATH
7.3 激活环境变量
 [home user1] source ~/.bashrc
7.4 测试版本
[home user1] gcc -v

结果图
参考(略有改动):知乎

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐