1、下載gcc源碼:https://ftp.gnu.org/gnu/gcc/
2、解壓源碼 tar zxvf gcc-8.3.0.tar.gz
3、查看安裝前依賴的軟件包: vim contrib/download_prerequisites
30 gmp='gmp-6.1.0.tar.bz2'
31 mpfr='mpfr-3.1.4.tar.bz2'
32 mpc='mpc-1.0.3.tar.gz'
33 isl='isl-0.18.tar.bz2'
4、下載對應的依賴包:
https://ftp.gnu.org/gnu/gmp/
https://ftp.gnu.org/gnu/mpfr/
https://ftp.gnu.org/gnu/mpc/
https://gcc.gnu.org/pub/gcc/infrastructure/
5、安裝的順序依次為:GMP,mpfr, mpc,isl 。最后安裝gcc
6、安裝依賴包:gmp
$ tar -jxvf gmp-6.1.0.tar.bz2
$ cd gmp-6.1.0
$ mkdir temp
$ cd temp
$ ../configure --prefix=/usr/local/gmp-6.1.0 -----指定安裝目錄
$ make
$ make install
7、安裝依賴包:mpfr
$ tar -zxvf mpfr-3.1.2.tar.gz
$ cd mpfr-3.1.2
$ mkdir temp
$ cd temp
$ ../configure --prefix=/usr/local/mpfr-3.1.0 --with-gmp=/usr/local/gmp-5.0.1
$ make
$ make install
其中--with=/usr/local/gmp-5.0.1就是依賴項, /usr/local/gmp-5.0.1是gmp的安裝目錄
8、安裝依賴包:mpc
$ tar -zxvf mpc-1.0.2.tar.gz
$ cd mpc-1.0.2
$ mkdir temp
$ cd temp
$ ../configure --prefix=/usr/local/mpc-1.0.2 --with-gmp=/usr/local/gmp-5.0.1 --with-mpfr=/usr/local/mpfr-3.1.0
$ make
$ make install
記得后面兩項的依賴項,也就是你的gmp和mpfr的安裝目錄
9、安裝依賴包:isl (isl只依賴gmp)
$ tar -zxvf isl-0.18.tar.bz2
$ cd isl-0.18
$ mkdir temp
$ cd temp
../configure --prefix=/usr/local/isl-0.18 --with-gmp=/usr/local/gmp-5.0.1
10、安裝gcc
首先查看服務器上原gcc安裝時的配置選項 : gcc -v
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
參照上面修改:主要修改--prefix為自己新的gcc安裝,gmp,mpfr,mpc,isl路徑指定為上面剛剛安裝好的
mkdir build
cd build
../configure --prefix=/opt/xxx .....
make -j100
make install
11、安裝完成后在對應目錄生成 /opt/xxx, 一堆目錄,這就是可執行文件和頭文件,還有一些庫文件。
12、安裝完成后,如何使用對應版本的gcc?
2種方法,
1、最簡單的臨時修改; 開一個shell終端,將PATH環境變量修改如下 export PATH=/opt/xxx:$PATH
這樣執行系統命令時,會優先到你的目錄下找找可執行文件,自然就走你的gcc
2、或者配置環境變量(這樣就應用到了所有新shell終端):
vim /etc/profile 寫入 export PATH=/opt/xxx:$PATH
3、修改、/usr/bin/gcc /usr/bin/g++的符號鏈接
ln -s /opt/xxx /bin/gcc /usr/bin/gcc
ln -s /opt/xxx /bin/g++ /usr/bin/g++
有可能還需要給cc,c++建立符號鏈接,看情況
注意編譯程序時系統可能會到/lib64/libstdc++.so.6 去鏈接lib,編譯過程中可以關注,給libstdc++.so.6也建立符號鏈接到自己的gcc目錄下對應的lib
13、找不到gmp、mpfr、mpc、isl怎么辦?
將路徑添加到vim /etc/ld.so.conf文件中,路徑一直到lib/
14、ldconfig 刷新庫cache
其他參考
參考:https://www.jianshu.com/p/444169a3721a
===============================一下為遇到的其他問題,安裝過程遇到問i,可以看下===================================================
3.5 gcc、g++、gcj設置
要想使用GCC 4.3.4的gcc等命令,簡單的方法就是把它的路徑${destdir}/bin放在環境變量PATH中。我不用這種方式,而是用符號連接的方式實現,這樣做的好處是我仍然可以使用系統上原來的舊版本的GCC編譯器。
而原來gcc的路徑是在usr/bin下。我們可以把gcc 4.3.4中的gcc、g++、gcj等命令在/usr/bin目錄下分別做一個符號連接:
$ cd /usr/bin
$ ln -s /usr/local/gcc-4.3.4/bin/gcc gcc434
$ ln -s /usr/local/gcc-4.3.4/bin/g++ g++434
$ ln -s /usr/local/gcc-4.3.4/bin/gcj gcj434
這樣,就可以分別使用gcc434、g++434、gcj434來調用GCC 4.1.2的gcc、g++、gcj完成對C、C++、JAVA程序的編譯了。同時,仍然能夠使用舊版本的GCC編譯器中的gcc、g++等命令。
3.6 庫路徑的設置
將${destdir}/lib路徑添加到環境變量LD_LIBRARY_PATH中,例如,如果GCC 4.3.4安裝在/usr/local/gcc-4.3.4目錄下,在RH Linux下可以直接在命令行上執行
$ export LD_LIBRARY_PATH=/usr/local/gcc-4.3.4/lib
最好添加到系統的配置文件中,這樣就不必要每次都設置這個環境變量了,在文件$HOME/.bash_profile中添加下面兩句:
LD_LIBRARY_PATH=:/usr/local/mpc-1.0.2/lib:/usr/local/gmp-5.0.1/lib:/usr/local/mpfr-3.1.2/lib:/usr/local/gcc-4.3.4/lib
export LD_LIBRARY_PATH
或者在/etc/bash_profile 下添加。
重啟系統設置生效,或者執行命令
$ source $HOME/.bash_profile
或者:
$ source /etc/bash_profile
用新的編譯命令(gcc412、g++412等)編譯你以前的C、C++程序,檢驗新安裝的GCC編譯器是否能正常工作。
完成了Linux安裝GCC,之后你就能輕松地編輯了。
在64位CentOS上編譯GCC-4.8時,出現了GCC: Linux gnu/stubs-32.h: No such file or directory的錯誤。
查資料得知,這是缺少32位的嵌入式C庫。在嵌入式開發環境配置時,也常遇到這個問題。
各平台的解決辦法:
Debian Linux:
$ sudo apt-get install libc6-dev
Ubuntu Linux:
$ sudo apt-get install libc6-dev-i386
OpenSUSE / Novell Suse Linux (SLES):
# zypper in glibc-devel-32bit
RHEL / Fedora / CentOS / Scientific Linux:
$ sudo yum install glibc-devel.i686
問題1:gcc源碼自身編譯,找不到庫的問題
gcc-7.3.0/host-x86_64-pc-linux-gnu/gcc/cc1: error while loading shared libraries: libisl.so.15: cannot open shared object file: No such file or directory
make[3]: *** [s-selftest] Error 1
make[3]: Leaving directory `/root/Downloads/gcc-7.3.0/host-x86_64-pc-linux-gnu/gcc'
make[2]: *** [all-stage1-gcc] Error 2
make[2]: Leaving directory `/root/Downloads/gcc-7.3.0'
make[1]: *** [stage1-bubble] Error 2
make[1]: Leaving directory `/root/Downloads/gcc-7.3.0'
# 在"/usr/local/lib"目錄下,怎么就找不到庫libisl.so。
解決方法是:加到"/etc/ld.so.conf"或用"LD_LIBRARY_PATH"。
vi /etc/ld.so.conf #添加庫的路徑/usr/local/lib/
然后終端命令行執行ldconfig,再重新編譯。
問題2:gcc升級完成之后,編譯項目工程時遇到的軟連接問題
/lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found
解決辦法:https://blog.csdn.net/libaineu2004/article/details/77100132