1. 程式人生 > >【Tools】gcc4.4升級到gcc4.8

【Tools】gcc4.4升級到gcc4.8

00.目錄

01. 簡介

由於gcc4.4不支援C++11新特性,所以將Redhat6.5中gcc4.4升級到gcc4.8. 本文介紹在系統無法連線網際網路的情況下,如何升級GCC。離線和線上升級的主要區別在於,如果可以聯網,在升級gcc前的需要安裝的依賴包,可以通過執行gcc安裝包下的指令碼自行下載安裝,免去了很多的麻煩。

環境:RedHat6.5

首先下載gcc安裝包,本文為gcc-4.8.5.tar.gz,解壓後,如果直接執行安裝目錄下的configure指令碼,可能會因為當前系統的GMP,MPFR,MPC的版本過低而丟擲如下的錯誤 這裡寫圖片描述

02. 軟體包下載

需要使用的安裝包為 gcc-4.8.5.tar.gz,gmp-5.0.5.tar.bz2,mpfr-3.0.1.tar.gz,mpc-1.0.1.tar.gz.

如果是在聯網環境,可以執行安裝目錄下的”./contrib/download_prerequisites”指令碼來下載相關的依賴。而由於我們是在區域網內,所以需要到因特網中下載這三個安裝包,然後逐個安裝:

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/. 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 rm $MPFR.tar.bz2 $GMP.tar.bz2 $MPC.tar.gz || exit 1

03. 安裝gmp

GMP是一個任意精度的開源算術庫,可用於符號整數,有理數,浮點數計算。

第一步: 解壓

[[email protected] tmp]# tar -xjvf gmp-5.0.5.tar.bz2

第二步: 檢查環境,生成對應檔案

[[email protected] tmp]# cd gmp-5.0.5 [[email protected] gmp-5.0.5]# ./configure

第三步: 編譯

[[email protected] gmp-5.0.5]# make -j4 [[email protected] gmp-5.0.5]# make check

第四步: 安裝

[[email protected] gmp-5.0.5]# make install

04. 安裝mpfr

mpfr主要為提供C/C++多精度浮點運算

第一步: 解壓

[[email protected] tmp]# tar -xjvf mpfr-3.0.1.tar.bz2

第二步: 檢查環境

[[email protected] tmp]# cd mpfr-3.0.1 [[email protected] mpfr-3.0.1]# ./configure –with-gmp-include=/usr/local/include –with-gmp-lib=/usr/local/lib

第三步: 編譯

[[email protected] mpfr-3.0.1]# make -j4 [[email protected] mpfr-3.0.1]# make check

第四步: 安裝

[[email protected] mpfr-3.0.1]# make install

05. 安裝mpc

第一步: 解壓

[[email protected] tmp]# tar -xzvf mpc-1.0.1.tar.gz

第二步: 檢查環境

[[email protected] tmp]# cd mpc-1.0.1 [[email protected] mpc-1.0.1]# ./configure

第三步: 編譯

[[email protected] mpc-1.0.1]# make -j4

第四步: 安裝

[[email protected] mpc-1.0.1]# make install

第五步: 配置 安裝後,它們的標頭檔案位於”/usr/local/include”,預設情況下程式可以自動找到該路徑;它們的動態庫位於”/usr/local/lib”,可在環境變數追加該路徑,此處就在當前使用者的環境變數上加上該路徑:

[[email protected] tmp]# vim ~/.bash_profile 新增如下內容: LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/local/lib64:/usr/lib64

使配置生效

[[email protected] tmp]# source ~/.bash_profile 這裡寫圖片描述

06. 安裝gcc

第一步: 解壓

[[email protected] tmp]# tar -xzvf gcc-4.8.5.tar.gz

第二步: 檢查環境 [[email protected] tmp]# cd gcc-4.8.5 [[email protected] gcc-4.8.5]# mkdir gcc-4.8.5-build [[email protected] gcc-4.8.5]# cd gcc-4.8.5-build/

可以參考gcc -v選項 注意: 如果此處出現關於java的錯誤, 可以將Java選項去掉

[[email protected] gcc-4.8.5-build]# ../configure –prefix=/usr –mandir=/usr/share/man –infodir=/usr/share/info –disable-multilib –enable-bootstrap –enable-shared –enable-threads=posix –enable-checking=release –with-system-zlib –enable-__cxa_atexit –disable-libunwind-exceptions –enable-gnu-unique-object –enable-languages=c,c++, –disable-dssi –disable-libjava-multilib –with-ppl –with-cloog –with-tune=generic –with-arch_32=i686 –build=x86_64-redhat-linux

這裡寫圖片描述

第三步: 編譯

[[email protected] gcc-4.8.5-build]# make -j4

第四步: 安裝

[[email protected] gcc-4.8.5-build]# make install

第五步: 測試 gcc測試 這裡寫圖片描述

g++測試 這裡寫圖片描述

驗證安裝 除了可以通過”gcc -v”檢視安裝後的gcc版本,還可以通過編寫C++11標準的程式來驗證,在編譯C++11程式時,應該加上”std=c++11”,否則預設是以C99進行編譯,將會丟擲錯誤。

07. 錯誤排查(可選 )

如果06出現老版本 就可以參考一下操作

安裝完成後,系統預設沒有修改環境變數,目前還是使用老版本的gcc。 設定使用新版gcc: ls /usr/local/bin | grep gcc 新增新GCC到可選項,倒數第三個是名字,倒數第二個引數為新GCC路徑,最後一個引數40為優先順序,設大一些之後就自動使用新版了

update-alternatives –install /usr/bin/gcc gcc /usr/local/gcc4.8/bin/i686-pc-linux-gnu-gcc 40

第二種方式: 建立軟連線

mkdir /usr/gcc447backup/ mv /usr/bin/{gcc,g++} /usr/gcc447backup ln -s /usr/local/gcc4.8/bin/gcc /usr/bin/gcc ln -s /usr/local/gcc4.8/bin/g++ /usr/bin/g++