编译安装 gcc 4.8.3

要用到 c++11,基础设施建设。。。做个记录

0. 系统环境:

 root@melos1305-B85M-DS3H:~# cat /etc/lsb-release
 DISTRIB_ID=Ubuntu
 DISTRIB_RELEASE=12.04
 DISTRIB_CODENAME=precise
 DISTRIB_DESCRIPTION="Ubuntu 12.04 LTS"
 root@melos1305-B85M-DS3H:~# uname -a
 Linux melos1305-B85M-DS3H 3.2.0-24-generic #37-Ubuntu SMP Wed Apr 25 08:43:22 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

1. 用root用户

 su

 

2. 下载gcc: 

 # wget -c http://gnu.mirror.iweb.com/gcc/gcc-4.8.3/gcc-4.8.3.tar.gz

  解压 gcc-4.8.3.tar.gz

# tar -xzvf gcc-4.8.3.tar.gz

  解压出gcc-4.8.3 ,创建gcc-4.8.3 的同级目录  mkdir gcc-bulid

3. 进入gcc-bulid 执行:

 # ../gcc-4.8.3/configure --prefix=/usr/local/gcc-4.8.3 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp-6.0.0/ --with-mpfr=/usr/local/mpfr-3.1.2/ --with-mpc=/usr/local/mpc-1.0.1/

  configure报错

  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

  

  编译 gcc 需要 gmp,mpfr,mpc,m4 下载

 # wget -c http://ftp.gnu.org/gnu/m4/m4-latest.tar.xz
 # wget -c http://www.mpfr.org/mpfr-current/mpfr-3.1.2.tar.gz
 # wget -c ftp://ftp.gnu.org/gnu/mpc/mpc-1.0.1.tar.gz
 # wget -c https://gmplib.org/download/gmp/gmp-6.0.0a.tar.lz.sig

  解压后分别新建同级文件夹mkdir gcc-build, m4-bulid, gmp-bulid, mpfr-bulid, mpc-bulid, gmp-bulid,在bulid文件夹里执行configure

  安装依赖关系:

     m4->gmp  

     mpfr->gmp

     mpc->gmp,mpfr

     gcc->gmp,mpfr,mpc 

  分别编译安装:     

 # m4, ../m4-xxx/configure;make;make check;   make install
 # gmp ../gmp-xxx/configure --prefix=/usr/local/gmp-xxx;  make;  make check;  make install
 # mpfr ../mpfr-xxx/configure --prefix=/usr/local/mpfr-xxx --with-gmp=/usr/local/gmp-xxx;   make;   make check;  make install
 # mpc  ../mpc-xxx/configure --prefix=/usr/local/mpc-xxx --with-gmp=/usr/local/gmp-xxx --with-mpfr=/usr/local/mpfr-xxx;   make;   make check;   make install

  同时  vi /etc/ld.so.conf

  添加  /usr/local/mpc-1.0.1/lib  

  ldconfig  

  进入 gcc-bulid 执行:

 # ../gcc-4.8.3/configure --prefix=/usr/local/gcc-4.8.3 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp-6.0.0/ --with-mpfr=/usr/local/mpfr-3.1.2/ --with-mpc=/usr/local/mpc-1.0.1/

   完成后, gcc-bulid文件夹里有makefile

4. 编译gcc:

 # make

  g++ 4.8.3 编译不过报错configure: error: cannot compute suffix of object files: cannot compile

  需要将mpc、gmp和mpfr目录加入到环境变量中,

 # vi /etc/profile

  文件末尾加上:export LD_LIBRARY_PATH=/usr/local/mpc-1.0.1/lib:/usr/local/gmp-6.0.0/lib:/usr/local/mpfr-3.1.2/lib

  注意修改目录,之后运行  

 # source /etc/profile

  使其立即生效即可

5. 安装gcc:  

1 # make install

6. 修改符号链接:

 # cd /usr/bin/
 # ln -si ln -si /usr/local/gcc-4.8.3/bin/gcc gcc      (修改gcc符号链接)
 # ln -si ln -si /usr/local/gcc-4.8.3/bin/g++ g++  (修改g++符号链接)

  最后查看下版本

root@melos1305-B85M-DS3H:~# g++ -v
使用内建 specs。
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/gcc-4.8.3/libexec/gcc/x86_64-unknown-linux-gnu/4.8.3/lto-wrapper
目标:x86_64-unknown-linux-gnu
配置为:../gcc-4.8.3/configure --prefix=/usr/local/gcc-4.8.3 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp-6.0.0/ --with-mpfr=/usr/local/mpfr-3.1.2/ --with-mpc=/usr/local/mpc-1.0.1/
线程模型:posix
gcc 版本 4.8.3 (GCC) 
root@melos1305-B85M-DS3H:~# gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/gcc-4.8.3/libexec/gcc/x86_64-unknown-linux-gnu/4.8.3/lto-wrapper
目标:x86_64-unknown-linux-gnu
配置为:../gcc-4.8.3/configure --prefix=/usr/local/gcc-4.8.3 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp-6.0.0/ --with-mpfr=/usr/local/mpfr-3.1.2/ --with-mpc=/usr/local/mpc-1.0.1/
线程模型:posix
gcc 版本 4.8.3 (GCC) 

7. 其他:

  1.  好像可以加也可以不加,看情况,g++调用不了动态库就加上,修改库的路径环境变量: 

# vi /etc/profile  
#    添加 export LD_LIBRARY_PATH=/usr/local/gcc-4.8.3/lib:/usr/local/mpc-1.0.1/lib:/usr/local/gmp-6.0.0/lib:/usr/local/mpfr-3.1.2/lib    
# source /etc/profile

    

  2.  如果遇到

melos1305@melos1305-B85M-DS3H:~/project/cocos2dx/cocos2d-x-3.1/build/linux-build/bin/cpp-empty-test$ ./cpp-empty-test 
./cpp-empty-test: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.18' not found (required by ./cpp-empty-test)

  std动态库版本跟不上,这修改/usr/lib/下的链接

# ln -si  /usr/local/gcc-4.8.3/lib/x86_64-linux-gnu/libstdc++.so.6.0.19 libstdc++.so.6        #链接到gcc-4.8.3的动态库

  挂链接,服务端的ubuntu是64位,所以lib64/libstd++.so.6的默认版本不够,需要支持到GLIBCXX_3.4.18以上, 查看GLIBCXX版本信息:

strings /usr/lib/x86_64-linux-gnu/libstdc++.so.6 | grep GLIBCXX
strings /usr/local/gcc-4.8.3/lib64/libstdc++.so.6.0.19 | grep GLIBCXX  

  

  3.  如果遇到编译cpp文件时遇到

root@melos1305-B85M-DS3H:/home/melos1305/lilu# /usr/local/gcc-4.8.3/bin/g++ string.cpp  
/usr/local/gcc-4.8.3/libexec/gcc/x86_64-unknown-linux-gnu/4.8.3/cc1plus: error while loading shared libraries: libmpc.so.3: cannot open shared object file: No such file or directory

  则vi /etc/ld.so.conf

  添加/usr/local/mpc-1.0.1/lib  

  ldconfig  

  要在mpc安装完之后补上

  

  4.  gcc的安装文件夹最好不要删掉,以后万一不小心搞坏了(比如挂g++,gcc,动态库的链接挂反了),make install 一下就恢复了, build目录里的 config.log 里记录当时执行configue脚本的配置

    

  5. 遇到 

    melos1305@melos1305-B85M-DS3H:~/project/lilu/mysql/mysqlSingleton$ ./MysqlDeal
    ./MysqlDeal: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.19' not found (required by ./MysqlDeal)

    http://stackoverflow.com/questions/19386651/how-to-fix-usr-lib-libstdc-so-6-version-glibcxx-3-4-15-not-found

    g++ 增加选项 -static-libstdc++

   

  6. 如果是ubuntu系统,可以直接安装

  http://askubuntu.com/questions/288335/ubuntu-13-04-with-gcc-g-gfortran-4-8-by-default/306472#306472

  The following should do the trick:

  sudo add-apt-repository ppa:ubuntu-toolchain-r/test
  sudo apt-get update
  sudo apt-get install g++-4.8

  sudo apt-get install g++-4.9

  Note, if you receive an error saying that add-apt-repository does not exist, you can install it via:

  sudo apt-get install software-properties-common

    

8. 参考:

1 http://bbs.chinaunix.net/thread-314284-1-1.html
2 http://blog.csdn.net/zjg555543/article/details/7421416
3 https://gcc.gnu.org/install/
4 https://www.gnu.org/software/gcc/
5 https://www.gnu.org/software/gcc/mirrors.html
原文地址:https://www.cnblogs.com/coolulu/p/4124803.html