编译安装GCC 5.2.0

https://blog.atime.me/note/install-gcc-5.2.0-from-source.html

 

记录编译GCC 5.2.0时遇到的问题和解决方法,以备日后查询。

平时使用的服务器是CentOS5,自带的gcc编译器还是8年前发布的4.1.2版本,完全没法写C++11的代码,因为不想升级操作系统,只好自己下载源码编译。

安装过程挺dan疼的,只好记录下来。

安装依赖库

GCC依赖于gmp 4.2+, mpfr 2.4+和mpc 0.8+,这里直接下载安装最新的版本。

为了省事,所有的库都直接装到/usr/local目录下的对应目录。(不要在生产环境上这么干)

安装gmp 6.0

wget https://gmplib.org/download/gmp/gmp-6.0.0a.tar.bz2
tar xvf gmp-6.0.0a.tar.bz2
cd gmp-6.0.0
./configure
make -j4
make check
make install

安装mpfr 3.1.3

mpfr依赖于gmp。

wget http://www.mpfr.org/mpfr-current/mpfr-3.1.3.tar.bz2
tar xvf mpfr-3.1.3.tar.bz2
cd mpfr-3.1.3
./configure --with-gmp-include=/usr/local/include \
    --with-gmp-lib=/usr/local/lib
make -j4
make check
make install

安装mpc 1.0.3

mpc依赖于gmp和mpfr。

wget ftp://ftp.gnu.org/gnu/mpc/mpc-1.0.3.tar.gz
tar xvf mpc-1.0.3.tar.gz
cd mpc-1.0.3
./configure --with-mpfr-include=/usr/local/include \
    --with-mpfr-lib=/usr/local/lib \
    --with-gmp-include=/usr/local/include \
    --with-gmp-lib=/usr/local/lib
make -j4
make check
make install

安装GCC

编译

建议先阅读下官方的安装文档

下载GCC并解压。

wget ftp://ftp.gnu.org/gnu/gcc/gcc-5.2.0/gcc-5.2.0.tar.bz2
tar xvf gcc-5.2.0.tar.bz2
cd gcc-5.2.0

先unset若干个系统变量,以免出现某些宏找不到的情况。

unset CPLUS_INCLUDE_PATH LIBRARY_PATH

配置GCC

./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 \
    --with-mpc-include=/usr/local/include \
    --with-mpc-lib=/usr/local/lib \
    --enable-languages=c,c++ \
    --enable-threads=posix \
    --disable-multilib

详细的配置项说明可参考安装文档,这里只编译c和c++的编译器。

然后make -j8,启用多线程编译。

测试

先安装dejagnu: yum install dejagnu

然后运行如下命令:

make -j8 check-gcc

查看测试结果:

./contrib/test_summary

安装

如果编译顺利通过,make install即可。

gcc和g++默认被安装到/usr/local/bin目录下,libgcc和libstdc++默认被安装到/usr/local/lib64(x64)。

记得更下下动态库缓存。

ldconfig

可能遇到的问题

XXXX not defined

遇到某个宏没有定义的情况,先unset C_INCLUDE_PATH再尝试。

braced spec is invalid

很dan疼的一个问题,搜遍了全网也没见有比较正式的解决方案。目前看上去比较靠谱的方法可参考这里,具体操作就是手动改一下某个specs文件。

我这里是host-x86_64-unknown-linux-gnu/gcc/specs,把其中所有的%:sanitize(xxx)改为fsanitize=xxx

测试C++11

写一个脑残的cpp测试下新安装的编译器。

#include <atomic>
#include <regex>
#include <iostream>
using namespace std;

int main() {
    atomic<long long> num(1L << 14);
    cout << ++num << endl;

    regex r("[0-9]+");
    string s("0abc11abc222cba");
    sregex_iterator ib(s.begin(), s.end(), r);
    sregex_iterator ie;
    cout << "search numbers in: " << s << endl;
    for (sregex_iterator i = ib; i != ie; ++i) {
        cout << "match: " << i->str() << endl;
    }
}

编译并运行:

/usr/local/bin/g++ -std=c++11 b.cpp -o b
LD_LIBRARY_PATH=/usr/local/lib64 ./b
posted @ 2016-06-26 16:13  zengkefu  阅读(8417)  评论(0编辑  收藏  举报