gcc的安裝與多版本gcc切換

注:

本文僅適用于安裝gcc-6.3 g++-6.3之類的,因為apt-get不支持安裝gcc-5以上多版本。如需安裝版本5之下的,請直接使用apt-get 安裝。參考別人鏈接:https://blog.csdn.net/zhangxin4832/article/details/79225394/ 。對這個鏈接里內容不解也可留言~

因為需要配置環境測試實驗,需要更換gcc測試哪個版本合適。所以有了此文。歡迎交流。

序:GCC依賴于gmp 4.2+, mpfr 2.4+和mpc 0.8+,并且安裝順序不可以改變。這里直接下載安裝最新的版本。總的說就是每一步都是

configure(區別最大)
make
sudo make install

這篇文章跟我思路很類似,出現錯誤可以參考
https://blog.csdn.net/anneqiqi/article/details/51725658
各版本gcc下載:http://ftp.gnu.org/gnu/gcc/

前期工作:因為反正最后都要添加環境變量,那么我們就第一步完成吧,以免后面報錯。

打開profile:sudo gedit /etc/profile
在文本末尾添加:(export后面沒有回車)

#gcc
export
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mpc/lib:/usr/local/gmp/lib:/usr/local/mpfr/lib:/usr/local/gcc/lib

1、安裝gmp 6.1.2

wget后面對應的是下載地址,其他版本的階段到gmp處即可以

wget https://gmplib.org/download/gmp/gmp-6.1.2.tar.xz
tar xvf gmp-6.1.2.tar.xz
cd gmp-6.1.2
./configure --prefix=/usr/local/gmp
make -j20 && make install
./configure --prefix=/usr/local/gmp

其中:configure --prefix=是指的安裝目錄,后文還有--with指的是依賴文件的目錄。prefix和with前面都是兩個-
configure 、make、make install是linux中安裝文件常見的三步驟

sudo make
sudo make install

make check

執行./configure –prefix=/usr/local/gmp 可能會出現如下錯誤

checking whether sscanf needs writable input… no
checking for struct pst_processor.psp_iticksperclktick… no
checking for suitable m4… configure: error: No usable m4 in $PATH or /usr/5bin (see config.log for reasons).

那就安裝m4

yum install m4

安裝成功后再次執行configure命令成功會顯示如下界面

config.status: linking mpn/x86_64/coreihwl/gmp-mparam.h to gmp-mparam.h
config.status: executing libtool commands
configure: summary of build options:

Version: GNU MP 6.1.2
Host type: haswell-pc-linux-gnu
ABI: 64
Install prefix: /usr/local/gmp
Compiler: gcc -std=gnu99
Static libraries: yes
Shared libraries: yes

2:安裝mpfr 3.1.5 mpfr依賴于gmp

wget http://www.mpfr.org/mpfr-current/mpfr-3.1.5.tar.gz
tar xvf mpfr-3.1.5.tar.gz
cd mpfr-3.1.5
./configure --prefix=/usr/local/mpfr --with-gmp=/usr/local/gmp
make -j20 && make install
./configure

make

sudo make install

3:安裝mpc 1.1.0 mpc依賴于gmp和mpfr

wget ftp://ftp.gnu.org/gnu/mpc/mpc-1.0.3.tar.gz
tar xvf mpc-1.0.3.tar.gz
cd mpc-1.0.3
./configure --prefix=/usr/local/mpc --with-gmp=/usr/local/gmp --with-mpfr=/usr/local/mpfr

make  
sudo make install
image.png
make

image.png
sudo make install

image.png

4:安裝GCC 6.3.0

wget ftp://ftp.gnu.org/gnu/gcc/gcc-6.3.0/gcc-6.3.0.tar.gz
tar xvf gcc-6.3.0.tar.gz
cd gcc-6.3.0
sudo ./configure --prefix=/usr/local/gcc-6.3 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp --with-mpfr=/usr/local/mpfr --with-mpc=/usr/local/mpc
#這步make時間比較長,很長~
make 
make install

#軟鏈接一下,其實這里軟鏈接完全都是為了支持多版本gcc
sudo ln -s /usr/local/gcc-6.3  /usr/local/gcc
sudo ln -s /usr/local/gcc/bin/  /usr/bin/
sudo ln -s /usr/local/gcc/bin/g++  /usr/bin/
image.png

5:gcc版本切換

安裝其他版本只需要重新進行第4步就可以,因為我覺得反正用的時候只用一種gcc,那么gcc依賴完全可以共用一份,如果依賴版本有異那么從1開始裝就可以。注意目錄就可以

安裝過程中可能會出現:

“checking for suffix of object files… configure: error: cannot compute suffix of object files: cannot compile
See `config.log’ for more details.
make[2]: * [configure-stage1-target-libgcc] Error 1
make[2]: Leaving directory `/tmp/gcc-6.3.0’
make[1]: * [stage1-bubble] Error 2
make[1]: Leaving directory `/tmp/gcc-6.3.0’
make: * [bootstrap] Error 2

解決方法:
sudo apt-get install m4
sudo apt-get install gcc-c++

實際解決辦法:編輯變量,把我們安裝的gmp,mpfr,mpc加進去
vi /etc/ld.so .conf

添加部分:#這個是默認系統的變量
/usr/local/lib
/usr/local/gmp/lib
/usr/local/mpfr/lib
/usr/local/mpc/lib
添加保存后記得更新動態庫的緩存:
sudo ldconfig -v

更新后再去重新編譯安裝。

備份系統默認的gcc版本
sudo mv /usr/bin/gcc /usr/bin/gcc-bak
sudo mv /usr/bin/g++ /usr/bin/g++-bak
sudo mv /usr/bin/c++ /usr/bin/c++-bak

創建新的gcc軟連接
sudo ln -sf /usr/local/gcc/bin/gcc /usr/bin/gcc
sudo ln -sf /usr/local/gcc/bin/c++ /usr/bin/c++
sudo ln -sf /usr/local/gcc/bin/g++ /usr/bin/g++
sudo ln -sf /usr/local/gcc/lib64/libstdc++.so.6.0.22 /usr/lib64/libstdc++.so.6

查看gcc版本:

[root@localhost ~]# gcc --version
gcc (GCC) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO

warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR

[root@localhost ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/gcc/libexec/gcc/x86_64-pc-linux-gnu/6.3.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ./configure –prefix=/usr/local/gcc –enable-threads=posix –disable-checking –disable-multilib –enable-languages=c,c++ –with-gmp=/usr/local/gmp –with-mpfr=/usr/local/mpfr –with-mpc=/usr/local/mpc
Thread model: posix
gcc version 6.3.0 (GCC)


參考:https://blog.csdn.net/u014608280/article/details/80569328

后記:

可能因為我返回安裝的原因,可以參考此文先卸載后安裝老版本4.77,再重新安裝新版本(gcc和g++同步裝)
https://blog.csdn.net/ppp036/article/details/51126468

有問題歡迎提問~

sudo ln -sri /usr/local/gcc/bin/* /usr/local/bin

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容