1、用GNU上下载对应版本的源码包到本地,官网:http://ftp.gnu.org/gnu/gcc/

  这里选用最新的包:

    wget http://ftp.gnu.org/gnu/gcc/gcc-7.1.0/gcc-7.1.0.tar.gz

2、解压对应的包,然后进入到gcc-7.1.0目录下:

    tar zxvf gcc-7.1.0.tar.gz

3、下载更新所需要的依赖包,执行:

    ./contrib/download_prerequisites

    下载完成后,会看到新生成的文件

    -rw-r--r--  1 root root 2383840 Jun 12 12:34 gmp-6.1.0.tar.bz2

    -rw-r--r--  1 root root 1279284 Jun 12 12:34 mpfr-3.1.4.tar.bz2

    -rw-r--r--  1 root root  669925 Jun 12 12:34 mpc-1.0.3.tar.gz

    -rw-r--r--  1 root root 1626446 Jun 12 12:34 isl-0.16.1.tar.bz2

    lrwxrwxrwx  1 root root      12 Jun 12 12:34 gmp -> ./gmp-6.1.0/

    lrwxrwxrwx  1 root root      13 Jun 12 12:34 mpfr -> ./mpfr-3.1.4/

    lrwxrwxrwx  1 root root      12 Jun 12 12:34 mpc -> ./mpc-1.0.3/

    lrwxrwxrwx  1 root root      13 Jun 12 12:34 isl -> ./isl-0.16.1/

  

   接下来安装对应的依赖包,依赖包可能存在一定依赖,请按照顺序进行安装,gmp->mpfr->mpc->isl。分别进入对应目录中,执行./configure ---> make ---> make install 即可。(这个顺序,亲测可行)

4、生成g++的makefile文件

    ./configure -enable-checking=release -enable-languages=c,c++ -disable-multilib

5、编译

    make

     使用make -j4,采用多核优化提高速度

6、安装

    make install


7、到此安装完毕,看到一下gcc,g++的版本

    [root@localhost gcc-7.1.0]# gcc --version

    gcc (GCC) 7.1.0

    Copyright (C) 2017 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.


    [root@localhost gcc-7.1.0]# g++ --version

    g++ (GCC) 7.1.0

    Copyright (C) 2017 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.

8、更新libstdc++.so.6的库

    从安装包里查找对应的库:    

    [root@localhost gcc-7.1.0]# find . -name libstdc++.so.6*

    ./prev-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.23

    ./prev-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6

    ./x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.23

    ./x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6

    ./stage1-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.23

    ./stage1-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6

    将libstdc++.so.6.0.23拷贝到/usr/lib64/目录下

    进行/usr/lib64/目录下,删除libstdc++.so.6(为一个软链接)

    将libstdc++.so.6.0.23软链接为libstdc++.so.6

    即:ln -s libstdc++.so.6.0.23  libstdc++.so.6

9、写个简单的Hello world测试一下    

    [root@localhost codes]# cat hello.cpp

    #include <iostream>

    #include <memory>

    

    void foo(std::shared_ptr<int> i)

    {

            (*i) ++;

    }

    

    int main()

    {

            auto pointer = std::make_shared<int>(10);

            foo(pointer);

    

            std::cout<<*pointer<<std::endl;

    

            return 0;

    }

    

    [root@localhost codes]#

    [root@localhost codes]# g++ hello.cpp -std=c++14

    [root@localhost codes]#

    [root@localhost codes]# ./a.out

     11

    [root@localhost codes]#

9、看来没问题,升级结束j_0051.gif