CentOS下gcc(g++)源码安装配置

1. 说明

CentOS7.5 系统自带 gcc(g++) 版本为4.8.5,当我们编译些比如 CGAL 源码时有高版本的 g++ 的需求。本篇记录了 gcc 10.2.0 的安装过程和常见问题。下面是软件版本号。

软件版本
CentOS7.5 x64
gmp6.2.0
mpfr4.1.0
mpc1.2.1
gcc10.2.0
  • /home 目录下创建gcc 目录,所有软件编译均在此目录下进行

[root@VM-0-9-centos ~]# cd /home
[root@VM-0-9-centos home]# mkdir gcc
[root@VM-0-9-centos home]# cd gcc

2. 编译安装gmp

命令如下

[root@VM-0-9-centos gcc]# yum install m4
[root@VM-0-9-centos gcc]# wget https://gmplib.org/download/gmp/gmp-6.2.0.tar.xz
[root@VM-0-9-centos gcc]# tar -xvf gmp-6.2.0.tar.xz
[root@VM-0-9-centos gcc]# cd gmp-6.2.0
[root@VM-0-9-centos gmp-6.2.0]# ./configure –enable-cxx
[root@VM-0-9-centos gmp-6.2.0]# make
[root@VM-0-9-centos gmp-6.2.0]# make check
[root@VM-0-9-centos gmp-6.2.0]# make install

有问题可参考下,比较全面(篇幅不长)。
https://blog.csdn.net/ShyLoneGirl/article/details/109579797

3. 编译安装mpfr

返回gcc 目录, 下载并解压mpfr

[root@VM-0-9-centos gmp-6.2.0]# cd ../
[root@VM-0-9-centos gcc]# wget https://www.mpfr.org/mpfr-current/mpfr-4.1.0.tar.xz
[root@VM-0-9-centos gcc]# tar -xvf mpfr-4.1.0.tar.xz

mpfr-4.1.0 目录编译

[root@VM-0-9-centos gcc]# cd mpfr-4.1.0/
[root@VM-0-9-centos mpfr-4.1.0]# ./configure  --with-gmp-include=/usr/local/include --with-gmp-lib=/usr/local/lib
[root@VM-0-9-centos mpfr-4.1.0]# make
[root@VM-0-9-centos mpfr-4.1.0]# make install

4. 编译安装mpc

返回gcc 目录, 下载并解压mpc

[root@VM-0-9-centos gmp-6.2.0]# cd ../
[root@VM-0-9-centos gcc]# wget https://ftp.gnu.org/gnu/mpc/mpc-1.2.1.tar.gz
[root@VM-0-9-centos gcc]# tar -xvf mpc-1.2.1.tar.gz

mpc-1.2.1 目录编译

[root@VM-0-9-centos gcc]# cd mpc-1.2.1/
[root@VM-0-9-centos mpc-1.2.1]# ./configure  --with-gmp-include=/usr/local/include --with-gmp-lib=/usr/local/lib --with-mpfr-include=/usr/local/include --with-mpfr-lib=/usr/local/lib
[root@VM-0-9-centos mpfr-4.1.0]# make
[root@VM-0-9-centos mpfr-4.1.0]# make install

5. gcc(g++)

5.1 编译安装

返回gcc 目录, 下载并解压gcc

[root@VM-0-9-centos gmp-6.2.0]# cd ../
[root@VM-0-9-centos gcc]# wget http://mirrors.tuna.tsinghua.edu.cn/gnu/gcc/gcc-10.2.0/gcc-10.2.0.tar.xz
[root@VM-0-9-centos gcc]# tar -xvf gcc-10.2.0.tar.xz

mpc-1.2.1 目录编译

[root@VM-0-9-centos gcc]# cd gcc-10.2.0
[root@VM-0-9-centos gcc-10.2.0]# ./configure --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++  --with-gmp-include=/usr/local/include --with-gmp-lib=/usr/local/lib --with-mpfr-include=/usr/local/include --with-mpfr-lib=/usr/local/lib --with-mpc-include=/usr/local/include --with-mpc-lib=/usr/local/lib
[root@VM-0-9-centos gcc-10.2.0]# make
[root@VM-0-9-centos gcc-10.2.0]# make install

其实gmp,mpfr,mpc的运行库和头文件都在环境变量里了 ,上面这大坨参数实际没必要跟了。
编译有点慢,多核cpu可在make 命令后加参数

[root@VM-0-9-centos gcc-10.2.0]# make -j4

5.1 替换老版本

使用时由于搜索环境变量顺序和别名的不同,有些软件编译时找的是 ccc++,当到/usr/bin/ 下时仍为老版本。

[root@VM-0-9-centos bin]# cc -v
Using built-in specs.
COLLECT_GCC=cc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
...
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
[root@VM-0-9-centos bin]# c++ -v
Using built-in specs.
COLLECT_GCC=c++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
...
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)

备份他们

[root@VM-0-9-centos bin]# mv cc cc.ori
[root@VM-0-9-centos bin]# mv c++ c++.ori

建立新版本的软连接

[root@VM-0-9-centos bin]# ln -s /usr/local/bin/gcc /usr/bin/cc
[root@VM-0-9-centos bin]# ln -s /usr/local/bin/c++ /usr/bin/c++

6. 可能出现问题

6.1 找不到动态链接库

在编译gcc 时进行make 命令时出现问题如下

/home/gcc/gcc-10.2.0/host-x86_64-pc-linux-gnu/gcc/cc1: error while loading shared libraries: libmpfr.so.6: cannot open shared object file: No such file or directory

解决办法
如果正确按上述步骤操作的话,查看/usr/local/lib/ 目录下有该文件,可能是没能更新环境变量,那么手动处理一下,

[root@VM-0-9-centos gcc-10.2.0]# vim /etc/ld.so.conf  

在新的一行中加入库文件所在目录

 /usr/local/lib  

然后执行即可解决

[root@VM-0-9-centos gcc-10.2.0]# ldconfig  

6.2 .py不是ELF文件

编译gcc 进行make install 命令时出现问题如下

/usr/bin:/root/bin:/sbin" ldconfig -n /usr/local/lib/../lib64
ldconfig: /usr/local/lib/../lib64/libstdc++.so.6.0.28-gdb.py is not an ELF file - it has the wrong magic bytes at the start.

解决办法
因为系统假定此目录下文件都是ELF文件,但实际它不是。解决办法就是不用解决,Jonathan Wakely 说的,他是libstdc++ 的维护者之一,如下。
https://gcc.gnu.org/legacy-ml/gcc-help/2014-08/msg00053.html

6.3 找不到GLIBCXX_3.4.21

使用g++编译程序后执行出现问题如下

[root@VM-0-9-centos xltest]# ./a.out
./a.out: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by ./a.out)

解决办法
新版本文件在/usr/local/lib64/ 内,而查找是从/usr/lib64 先的,因此最好是将新文件拷贝到/usr/lib64 并创建软连接。

[root@VM-0-9-centos xltest]# cp /usr/local/lib64/libstdc++.so.6  /usr//lib64

详细说明请查看
https://blog.csdn.net/ShyLoneGirl/article/details/109594054

.
.
.
.
.
.


桃花仙人种桃树,又摘桃花换酒钱_

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值