5

Centos7编译安装gcc8.3.0

 2 years ago
source link: https://crazyrico.github.io/posts/2020/04/23/7c0492b6.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Centos7编译安装gcc8.3.0

发表于

2020-04-23

更新于 2020-05-06 分类于 Python

阅读次数:
本文字数: 2.7k 阅读时长 ≈ 2 分钟

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

20200429164538.jpg

之前我们在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 ; \
fi
Could not import runpy module
Traceback (most recent call last):
File "/opt/Python-3.8.2/Lib/runpy.py", line 15, in <module>
import importlib.util
File "/opt/Python-3.8.2/Lib/importlib/util.py", line 14, in <module>
from contextlib import contextmanager
File "/opt/Python-3.8.2/Lib/contextlib.py", line 4, in <module>
import _collections_abc
SystemError: <built-in function compile> returned NULL without setting an error
generate-posix-vars failed
make[1]: *** [pybuilddir.txt] Error 1
make[1]: Leaving directory `/opt/Python-3.8.2'
make: *** [profile-opt] Error 2

查看本机gcc版本

# gcc --version
gcc (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 NO
warranty; 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 -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/gcc/libexec/gcc/x86_64-pc-linux-gnu/8.3.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --prefix=/usr/local/gcc --enable-languages=c,c++ --disable-multilib
Thread model: posix
gcc 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安装失败的问题。

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

https://bugs.python.org/issue35074
https://www.jianshu.com/p/444169a3721a

-------------本文结束感谢您的阅读-------------
Crazy Rico wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK