Centos下gcc的安裝、gcc的更新、gcc安裝過程中報錯:make[1]: *** [stage1-bubble] 錯誤 2

最近的一個項目中需要用到gcc編譯一些源碼,但是由於服務器系統是Centos,和Ubuntu還是有些區別的,Centos管理庫包的工具是yum,我使用yum安裝gccyum install gcc-c++,然而安裝的版本是4.8.5,當你更新的時候,更新完依舊是4.8.5,奔潰!但是,最新版本的gcc已經到10.1.0版本啦!

無奈,只能從源碼開始安裝

1 下載gcc的源碼

1.1 源碼下載地址:

點我——》帶你去:https://ftp.gnu.org/gnu/gcc/

1、下載gcc:我下載版本gcc-7.5.0(根據自己需要下載指定版本)

wget https://ftp.gnu.org/gnu/gcc/gcc-7.5.0/gcc-7.5.0.tar.gz

注意:如果下載比較慢,你就直接在瀏覽器中下載

2、解壓下載的源碼壓縮包

tar -zxf gcc-7.5.0.tar.gz

3、進入源碼目錄

cd gcc-7.5.0.tar.gz

1.2 下載gcc的依賴庫包

主要依賴庫包如下:

gmp ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.1.0.tar.bz2
mpfr ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-3.1.4.tar.bz2
mpc ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.0.3.tar.gz
isl ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.16.1.tar.bz2

下載gcc依賴
當然不一定是這幾個版本,無需去查找,只要在進入源碼目錄中執行如下命令,就會自動下載這幾個依賴:

./contrib/download_prerequisites

在這裏插入圖片描述
下載的四個依賴分別爲:

  • gmp-6.1.0.tar.bz2
  • mpfr-3.1.4.tar.bz2
  • mpc-1.0.3.tar.gz
  • isl-0.16.1.tar.bz2

2、解壓編譯依賴

解壓依賴文件命令:

  • tar jxvf gmp-6.1.0.tar.bz2
  • tar jxvf mpfr-3.1.4.tar.bz2
  • tar zxvf mpc-1.0.3.tar.gz
  • tar jxvf isl-0.16.1.tar.bz2

2.1 解壓編譯依賴gmp-6.1.0.tar.bz2

tar jxvf gmp-6.1.0.tar.bz2
cd gmp-6.1.0
./configure --prefix=/usr/local/gmp
make && make install

注意:
如果在執行./configure --prefix=/usr/local/gmp報錯:error: No usable m4 in $PATH or /usr/5bin (see config.log for reasons).,說明沒有安裝m4,使用yum install m4,然後再執行即可!

2.2 解壓編譯依賴mpfr-3.1.4.tar.bz2

tar jxvf mpfr-3.1.4.tar.bz2
cd mpfr-3.1.4
./configure --prefix=/usr/local/mpfr
make && make install

如果在執行./configure --prefix=/usr/local/mpfr報錯:configure: error: gmp.h can't be found, or is unusable.,是因爲缺少lipgmp庫,使用:yum install gmp-devel.x86_64安裝即可

2.3 解壓編譯依賴mpc-1.0.3.tar.gz

tar zxvf mpc-1.0.3.tar.gz
cd mpc-1.0.3
./configure --prefix=/usr/local/mpc
make && make install

如果在執行./configure --prefix=/usr/local/mpc報錯:error: libmpfr not found or uses a different ABI (including static vs shared)..,是因爲缺少lipgmp庫,使用:yum install mpfr-devel.x86_64安裝即可

2.4 解壓編譯依賴isl-0.16.1.tar.bz2

tar jxvf isl-0.16.1.tar.bz2
cd isl-0.16.1
./configure --prefix=/usr/local/isl
make && make install

2.5 添加依賴的環境變量

添加環境變量

vi ~/.bashrc
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mpc/lib:/usr/local/gmp/lib:/usr/local/mpfr/lib/:/usr/local/isl/lib
source ~/.bashrc

需要重啓系統,讓環境變量生效!!!

3 編譯gcc

cd gcc-7.5.0
./configure --prefix=/usr/local/gcc --with-gmp=/usr/local/gmp/ --with-mpfr=/usr/local/mpfr/ --with-mpc=/usr/local/mpc/ --with-isl=/usr/local/isl  --with-system-zlib --disable-multilib
make && make install

如果執行make && make install編譯報錯,如下:

.....
ocal/isl/include  -o lto-compress.o -MT lto-compress.o -MMD -MP -MF ./.deps/lto-compress.TPo ../.././gcc/lto-compress.c
../.././gcc/lto-compress.c:34:18: 致命錯誤:zlib.h:沒有那個文件或目錄
 #include <zlib.h>
                  ^
編譯中斷。
make[3]: *** [lto-compress.o] 錯誤 1
make[3]: 離開目錄“/HDD/tools/gcc-7.5.0/host-x86_64-pc-linux-gnu/gcc”
make[2]: *** [all-stage1-gcc] 錯誤 2
make[2]: 離開目錄“/HDD/tools/gcc-7.5.0”
make[1]: *** [stage1-bubble] 錯誤 2
make[1]: 離開目錄“/HDD/tools/gcc-7.5.0”
make: *** [all] 錯誤 2
(base) [root@localhost gcc-7.5.0]#

安裝zlibyum install zlib-devel.x86_64

編譯的過程比較久,大概編譯了三個多小時,建議把:make && make install分開執行,通過多進程執行,如下:

make -j8
make install

編譯完之後的結果:

.....
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/local/gcc/lib/../lib64

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
make[4]: 對“install-data-am”無需做任何事。
make[4]: 離開目錄“/HDD/tools/gcc-7.5.0/x86_64-pc-linux-gnu/libatomic”
make[3]: 離開目錄“/HDD/tools/gcc-7.5.0/x86_64-pc-linux-gnu/libatomic”
make[2]: 離開目錄“/HDD/tools/gcc-7.5.0/x86_64-pc-linux-gnu/libatomic”
make[1]: 離開目錄“/HDD/tools/gcc-7.5.0(base) [root@localhost gcc-7.5.0]# gcc -v
使用內建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目標:x86_64-redhat-linux
配置爲:../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
線程模型:posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
(base) [root@localhost gcc-7.5.0]#     

但是我們在:/usr/local/gcc/bin下可以看到已經生成可執行文件gcc,並且能夠查到版本是:7.5.0
在這裏插入圖片描述

(base) [root@localhost bin]# which gcc
/usr/bin/gcc
(base) [root@localhost bin]# cd /usr/bin
(base) [root@localhost bin]# ./gcc -v
使用內建 specs。
COLLECT_GCC=./gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目標:x86_64-redhat-linux
配置爲:../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
線程模型:posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
(base) [root@localhost bin]#

系統自帶的gcc版本的可執行文件爲:/usr/bin/gcc,我把這個可執行文件gcc備份成gcc_4.8.5

mv gcc gcc_4.8.5

然後把我自己安裝的:gcc-7.5.0 可執行文件鏈接到該目錄下:

ln -s /usr/local/gcc/bin/gcc /usr/bin/gcc

然後測試成功:

(base) [root@localhost ~]# gcc -v
使用內建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/gcc/libexec/gcc/x86_64-pc-linux-gnu/7.5.0/lto-wrapper
目標:x86_64-pc-linux-gnu
配置爲:./configure --prefix=/usr/local/gcc --with-gmp=/usr/local/gmp/ --with-mpfr=/usr/local/mpfr/ --with-mpc=/usr/local/mpc/ --with-isl=/usr/local/isl --with-system-zlib --disable-multilib
線程模型:posix
gcc 版本 7.5.0 (GCC)
(base) [root@localhost ~]#

同樣,如果你需要使g++,一樣建立軟連接配置

參考1:https://my.oschina.net/u/4393384/blog/3807031/print
參考2:https://blog.csdn.net/zr1076311296/article/details/51334538
參考3:https://bbs.huaweicloud.com/forum/thread-19219-1-1.html
參考4:https://www.cnblogs.com/devilmaycry812839668/p/10351763.html # 查看安裝幾個gcc
參考5:https://my.oschina.net/u/2306127/blog/538139 # 版本切換,我測試並並不可以,我的文件好像有些沒有

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章