CentOS上源码安装GCC 4.8.2

gcc --version  # 查看gcc版本
sudo yum update gcc -y  # 只能升到4.4.7

1) 前提

参考Prerequisites for GCC,需要GMP, MPFR, MPC, ISL, CLooG。

我先查看了下系统,发现原生就装了GMP等,如下命令。但之后"gcc ./configure"有错,仍需要下载安装GMP等。详见"Issue 1"。

yum list installed gmp  # 查看gmp安装了没

ps: 上述问题,应该是要"yum install gmp-devel"来安装开发环境就可以了。原本只有运行环境,所以是"Issue 1"里提到的找不到头文件。MPFR,MPC等也同样。

2) 准备

缺少的都可以先到ftp://gcc.gnu.org/pub/gcc/infrastructure/下找找看。

# 创建个目录,都下载到这里
mkdir -p ~/Env/gcc; cd ~/Env/gcc

# GMP, MPFR, MPC
wget https://ftp.gnu.org/gnu/gmp/gmp-5.1.3.tar.bz2
wget http://www.mpfr.org/mpfr-current/mpfr-3.1.2.tar.gz
wget ftp://ftp.gnu.org/gnu/mpc/mpc-1.0.2.tar.gz
tar jxvf gmp-5.1.3.tar.bz2
tar zxvf mpfr-3.1.2.tar.gz
tar zxvf mpc-1.0.2.tar.gz
# 都直接安装到默认路径下
cd gmp-5.1.3
./configure --enable-cxx CPPFLAGS=-fexceptions  # 选项原因"Issue 2"
make; make install
cd ../mpfr-3.1.2; ./configure
make; make install
cd ../mpc-1.0.2; ./configure
make; make install
cd ..

如果GMP、MPFR、MPC在"./configure"时用"--prefix"指定了安装路径,则"gcc ./configure"需要加上"--with-xxx"等选项。由GMP > MPFR > MPC依赖顺序,也需要加上"--with-xxx"选项。可见参考34。

之后"gcc ./configure"(见"3) 安装"),我这儿仍有错,ISL与CLooG也要安装:

sudo gedit config.log  # 查看日志,搜索"error"

# issue: configure: error: C++ compiler missing or inoperational
# 没有C++编译器
yum install gcc-c++

# issue: conftest.cpp:11:2: error: #error -static-libstdc++ not implemented
# 没有C,C++静态库
yum install glibc-static libstdc++-static -y
# 但报"No package libstdc++-static available",即没有-static-libstdc++源,问题仍存在。
# "checking whether g++ accepts -static-libstdc++ -static-libgcc"时报出的,可忽略。

# issue: conftest.c:10:25: error: isl/version.h: No such file or directory
# 没有ISL
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.12.2.tar.bz2
tar jxvf isl-0.12.2.tar.bz2
cd isl-0.12.2; ./configure
make; make install
cd ..

# issue: ./conftest: error while loading shared libraries: libisl.so.10: cannot open shared object file: No such file or directory
# 在"/usr/local/lib"目录下,怎么就它找不到。加到"/etc/ld.so.conf"或用"LD_LIBRARY_PATH"。
vi /etc/ld.so.conf  # 添加"/usr/local/lib"
ldconfig  # 重建/etc/ld.so.cache
# 自问:于**Issue 1**里,已经单独在"/etc/ld.so.conf.d"下建个"*.conf"添加过"/usr/local/lib",怎么没效果呢?
# 自答:之后安装的ISL,没重新ldconfig,所以找不到了?当时没查,不能确认。用以下命令:
strings /etc/ld.so.cache | grep libisl  # 查看
# 之后,删除了"/etc/ld.so.conf"内的添加,也不再有该问题。

# issue: conftest.c:10:27: error: cloog/version.h: No such file or directory
# 没有CLooG
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/cloog-0.18.1.tar.gz
tar zxvf cloog-0.18.1.tar.gz
cd cloog-0.18.1; ./configure
make; make install
cd ..

# issue: conftest.c:15: error: 'choke' undeclared (first use in this function)
# "checking for version 0.17.0 of CLooG"失败报出的,没问题。

如果安装时都自定义了路径,记得要将库路径都添加到"/etc/ld.so.conf"内。

3) 安装

# GCC
wget ftp://ftp.gnu.org/gnu/gcc/gcc-4.8.2/gcc-4.8.2.tar.gz  # 下载gcc源码
tar zxvf gcc-4.8.2.tar.gz  # 解压源码

cd gcc-4.8.2  # 进入源码目录
# 生成Makefile,./configure --help看帮助,或看参考2
./configure \
--prefix=/usr/local/gcc-4.8.2 \
--enable-languages=c,c++,go \
--disable-multilib

make; make install  # 编译并安装

# 重建gcc软链接
cd /usr/bin
mv gcc gcc4.4.7
mv g++ g++4.4.7
ln -s /usr/local/gcc-4.8.2/bin/gcc gcc
ln -s /usr/local/gcc-4.8.2/bin/g++ g++

# 添加gcc的man路径
vi /etc/man.config
# 添加"MANPATH /usr/local/gcc-4.8.2/share/man"
# 由于未卸载旧的,我这"man gcc"可以查看帮助,所以未添加。
# 如果再添加,不确定会有什么影响。

4) 问题

Issue 1:

king for the correct version of gmp.h... no configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+. Try the --with-gmp, --with-mpfr and/or --with-mpc options to specify their locations.

开始没注意"king for the correct version of gmp.h... no",先如下查了gmp等的位置。

rpm -qa|grep gmp  # 查看gmp安装了哪些
rpm -ql gmp-4.3.1-7.el6_2.2.i686  # 查看gmp安装位置

然后添加"--with-gmp"等,没效果。

难道库搜索路径没包括"/usr/lib",却包括了"/usr/local/lib"(之后安装了GMP等后,却能通过"./configure")?

于是,查看"/etc/ld.so.conf",其"include ld.so.conf.d/*.conf",但继续查看发现都不包含上述两路径,不是很明白。然后,自行添加了下。

cd /etc/ld.so.conf.d
touch mine.conf  # 或用vi,发现我不会用了
gedit mine.conf  # 添加/usr/lib与/usr/local/lib
ldconfig  # 重建/etc/ld.so.cache

注意,安装ISL后,执行ldconfig会有如下错误,重装换个低版本也一样。试着解决没成功,然后忽视了。类似提问:Pacman/ldconfig - wrong magic bytes。

ldconfig: /usr/local/lib/libisl.so.10.2.2-gdb.py is not an ELF file - it has the wrong magic bytes at the start.

vi命令参考[1][2]。

之后,发现GMP等头文件没有:

find / | grep gmp.h

所以还是要安装的。

Issue 2:

# 没有PPL
wget ftp://ftp.cs.unipr.it/pub/ppl/releases/1.1/ppl-1.1.tar.bz2
tar jxvf ppl-1.1.tar.bz2
cd ppl-1.1; ./configure
make; make install
cd ..

"ppl ./configure"时,

如果"gmp ./configure"未设置"--enable-cxx":

checking for the GMP library version 4.1.3 or above... no configure: error: Cannot find GMP version 4.1.3 or higher. GMP is the GNU Multi-Precision library: see http://www.swox.com/gmp/ for more information. When compiling the GMP library, do not forget to enable the C++ interface: add --enable-cxx to the configuration options.

如果"gmp ./configure"未设置"CPPFLAGS=-fexceptions":

configure: WARNING: CANNOT PROPAGATE EXCEPTIONS BACK FROM GMP: *** MEMORY EXHAUSTION MAY RESULT IN ABRUPT TERMINATION. *** This is OK, if you do not plan to use the bounded memory capabilities *** offered by the PPL. Otherwise, if you are using GCC or the Intel C/C++ *** compiler, please make sure you use a version of GMP compiled with the *** `-fexceptions' compiler option. *** To build such a version, you can configure GMP as follows: *** CPPFLAGS=-fexceptions ./configure --enable-cxx --prefix=/usr/local

按要求重编了GMP,但以上这个警号还会在。

"ppl make"时,

mp_std_bits.defs.hh:47: error: redefinition of 'class std::numeric_limits<__gmp_expr<__mpz_struct [1], __mpz_struct [1]> >' /usr/local/include/gmpxx.h:3271: error: previous definition of 'class std::numeric_limits<__gmp_expr<__mpz_struct [1], __mpz_struct [1]> >' mp_std_bits.defs.hh:108: error: redefinition of 'class std::numeric_limits<__gmp_expr<__mpq_struct [1], __mpq_struct [1]> >' /usr/local/include/gmpxx.h:3308: error: previous definition of 'class std::numeric_limits<__gmp_expr<__mpq_struct [1], __mpq_struct [1]> >'

ppl-0.11与gmp-5.1.3有冲突,改为ppl-1.1即可。

ppl-0.11: ftp://gcc.gnu.org/pub/gcc/infrastructure/ppl-0.11.tar.gz

5) 测试

test.cc:

#include <iostream>
#include <string>
#include <vector>

template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec)
{
    for (auto& el : vec)
    {
        os << el << ' ';
    }
    return os;
}

int main()
{
    std::vector<std::string> words = {
        "Hello", "from", "GCC", __VERSION__, "!"
    };
    std::cout << words << std::endl;
}

然后:

g++ -std=c++11 test.cc -o test
./test

输出:"Hello from GCC 4.8.2 !"。

6) 参考

  1. Prerequisites for GCC
  2. Installing GCC: Configuration
  3. CentOS6.4下源码安装gcc-4.8.1
  4. CentOS6.2下编译gcc4.8.2
  5. GCC 4.8.2 编译安装小记