非Root权限下gcc(g++)的安装【Centos】

有些时候,实验或者项目需要特定的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

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

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值