天天看点

centos7离线安装gcc_Centos7编译安装gcc8.3.0

前言

GCC(GNU Compiler Collection,GNU编译器套件)是由GNU开发的编程语言译器。GNU编译器套件包括C、C++、 Objective-C、 Fortran、Java、Ada和Go语言前端,也包括了这些语言的库(如libstdc++,libgcj等。)

centos7离线安装gcc_Centos7编译安装gcc8.3.0

之前我们在Centos7中编译安装Python时, 添加[–enable-optimizations] 参数,由于GCC版本较低会报错安装失败

gcc -pthread     -Xlinker -export-dynamic -o python Programs/python.o libpython3.8.a -lcrypt -lpthread -ldl  -lutil -lm   -lm ./python -E -S -m sysconfig --generate-posix-vars ;if test $? -ne 0 ; then echo "generate-posix-vars failed" ; rm -f ./pybuilddir.txt ; exit 1 ; fiCould not import runpy moduleTraceback (most recent call last):  File "/opt/Python-3.8.2/Lib/runpy.py", line 15, in     import importlib.util  File "/opt/Python-3.8.2/Lib/importlib/util.py", line 14, in     from contextlib import contextmanager  File "/opt/Python-3.8.2/Lib/contextlib.py", line 4, in     import _collections_abcSystemError:  returned NULL without setting an errorgenerate-posix-vars failedmake[1]: *** [pybuilddir.txt] Error 1make[1]: Leaving directory `/opt/Python-3.8.2'make: *** [profile-opt] Error 2
           

查看本机gcc版本

# gcc --versiongcc (GCC) 4.9.2 20150212 (Red Hat 4.9.2-6)Copyright (C) 2014 Free Software Foundation, Inc.This is free software; see the source for copying conditions.  There is NOwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
           

在gcc版本较低的情况下可以通过两种方式安装成功Python

  1. disable –enable-optimizations option. the software can be compile and install successful.
  2. upgrade gcc version to 8.2.0.

安装gcc8.3

这里我们通过升级gcc来解决上面的报错问题

下载gcc源码包

# cd /usr/local/src# wget http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gcc-8.3.0/gcc-8.3.0.tar.xz
           

解压

# tar -xvJf gcc-8.3.0.tar.xz
           

下载依赖包

# cd gcc-8.3.0/# ./contrib/download_prerequisites
           

执行命令后它会自动下载mpfr、gmp、mpc isl这4个库

编译安装

# mkdir build# cd build# ../configure --prefix=/usr/local/gcc --enable-languages=c,c++ --disable-multilib# make && make install
           

编译安装时间确实很长,可以去看部电影再回来O(∩_∩)O哈哈~

修改软链接

备份下之前的版本,创建指向gcc8.3的软链接

# mv /usr/bin/gcc /usr/bin/gcc_old# mv /usr/bin/g++ /usr/bin/g++_old# ln -s /usr/local/gcc/bin/gcc /usr/bin/gcc# ln -s /usr/local/gcc/bin/g++ /usr/bin/g++
           

查看gcc版本

# gcc -vUsing built-in specs.COLLECT_GCC=gccCOLLECT_LTO_WRAPPER=/usr/local/gcc/libexec/gcc/x86_64-pc-linux-gnu/8.3.0/lto-wrapperTarget: x86_64-pc-linux-gnuConfigured with: ../configure --prefix=/usr/local/gcc --enable-languages=c,c++ --disable-multilibThread model: posixgcc version 8.3.0 (GCC)
           

这时我们的gcc版本已经显示为8.3了,再次回到Python安装,就可以愉快的玩耍了

# cd Python-3.8.2/# ./configure --prefix=/usr/local/python3 --enable-optimizations --with-ssl# make && make install
           

使用参数 –enable-optimizations 安装Python会慢一些,确实通过升级gcc版本可以解决上面的Python安装失败的问题。

# python3Python 3.8.2 (default, Apr 23 2020, 08:38:06) [GCC 8.3.0] on linuxType "help", "copyright", "credits" or "license" for more information.>>>
           

参考

https://bugs.python.org/issue35074

https://www.jianshu.com/p/444169a3721a

-------------本文结束感谢您的阅读-------------

centos7离线安装gcc_Centos7编译安装gcc8.3.0

欢迎您扫一扫上面的微信公众号,订阅我的博客!