Ubuntu中编译GCC4.9.0

编译环境

此次编译使用的是Windows Azure的虚拟机,基本的配置信息是这样的。

项目信息
操作系统Ubuntu 14.04 LTS (GNU/Linux 3.13.0-27-generic x86_64)
处理器频率(MHz)2094.651
处理器核心数2
内存(GB)3.5

准备工作

1.一个支持ISO C++98的C++编译器

文使用g++作为编译器,这可以通过Ubuntu的包管理器apt-get来安装,在Ubuntu 14.04 LTS中安装得到的gcc/g++版本是4.8.2。

1
2
3
4
$ sudo apt-get update
$ sudo apt-get install g++
$ g++ --version
g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2

2.一个POSIX兼容的Shell或GNU bash

本文使用的是GNU bash

1
2
3
4
$ echo $SHELL
/bin/bash
$ /bin/bash --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)

3.一个POSIX或SVR4 awk

Ubuntu 14.04 LTS自带了GNU Awk 4.0.1,如果没有可以通过apt-get安装GNU Awk

1
$ sudo apt-get install gawk

4.GNU make 3.80 (或更高版本)

1
2
3
$ sudo apt-get install make
$ make --version
GNU Make 3.81

5.GNU tar 1.14 (或更高版本)

用于解压源代码,通常linux发行版都自带了。

1
2
3
$ sudo apt-get install tar
$ tar --version
tar (GNU tar) 1.27.1

编译GCC需要的支持库

1.GNU Multiple Precision Library (GMP) 4.3.2 (或更高版本)

编译GMP前需要先安装m4

1
$ sudo apt-get install m4

编译GMP并将其安装到/usr/local/gmp-6.0.0

1
2
3
4
5
6
7
8
9
10
$ wget https://gmplib.org/download/gmp/gmp-6.0.0a.tar.lz
$ sudo apt-get install lzip
$ tar -xvf gmp-6.0.0a.tar.lz
$ cd gmp-6.0.0
$ ./configure --prefix=/usr/local/gmp-6.0.0
$ make
$ sudo make install
$ ls /usr/local/gmp-6.0.0
include lib share
$ cd ..

2.MPFR Library 2.4.2 (或更高版本)

编译MPFR并将其安装到/usr/local/mpfr-3.1.2中。因为MPFR依赖GMP,而我在前面并不是将GMP安装到库的默认搜索路径下的,所以需要通过–with-gmp=path来指明GMP库所在的位置–with-gmp=path等价于–with-gmp-include=path/include和–with-gmp-lib=path/lib。

1
2
3
4
5
6
7
8
9
$ wget http://www.mpfr.org/mpfr-current/mpfr-3.1.2.tar.xz
$ tar -xvf mpfr-3.1.2.tar.xz
$ cd mpfr-3.1.2
$ ./configure --prefix=/usr/local/mpfr-3.1.2 --with-gmp=/usr/local/gmp-6.0.0
$ make
$ sudo make install
$ ls /usr/local/mpfr-3.1.2
include lib share
$ cd ..

3.MPC Library 0.8.1 (或更高版本)

编译MPC并将其安装到/usr/local/mpc-1.0.2

1
2
3
4
5
6
7
8
9
$ wget ftp://ftp.gnu.org/gnu/mpc/mpc-1.0.2.tar.gz
$ tar -xvf mpc-1.0.2.tar.gz
$ cd mpc-1.0.2
$ ./configure --prefix=/usr/local/mpc-1.0.2 --with-gmp=/usr/local/gmp-6.0.0 --with-mpfr=/usr/local/mpfr-3.1.2
$ make
$ sudo make install
$ ls /usr/local/mpc-1.0.2
include lib share
$ cd ..

4.ISL Library 0.12.2

编译ISL并将其安装到/usr/local/isl-0.12.2,注意这里指定gmp的安装目录要使用–with-gmp-prefix。

1
2
3
4
5
6
7
8
9
$ wget ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.12.2.tar.bz2
$ tar -xvf isl-0.12.2.tar.bz2
$ cd isl-0.12.2
$ ./configure --prefix=/usr/local/isl-0.12.2 --with-gmp-prefix=/usr/local/gmp-6.0.0
$ make
$ sudo make install
$ ls /usr/local/isl-0.12.2
include lib
$ cd ..

5.CLooG 0.18.1

编译CLooG并将其安装到/usr/local/cloog-0.18.1。

1
2
3
4
5
6
7
8
9
$ wget ftp://gcc.gnu.org/pub/gcc/infrastructure/cloog-0.18.1.tar.gz
$ tar -xvf cloog-0.18.1.tar.gz
$ cd cloog-0.18.1
$ ./configure --prefix=/usr/local/cloog-0.18.1 --with-gmp-prefix=/usr/local/gmp-6.0.0 --with-isl-prefix=/usr/local/isl-0.12.2
$ make
$ sudo make install
$ ls /usr/local/cloog-0.18.1
bin include lib
$ cd ..

编译GCC4.9.0

准备工作做完后就可以开始编译GCC4.9.0了。首先从 https://gcc.gnu.org/mirrors.html 下载GCC4.9.0的源码并解压。
gcc的configure有很多配置选项,这里我只配置了支持C和C++语言,禁用multilib以及安装到/usr/local/gcc-4.9.0。make -j[num]的num通常设置为CPU处理器数的两倍。编译GCC需要几个小时的时间,期间可以去看部电影。

1
2
3
4
$ cd gcc-4.9.0
$ mkdir build && cd build
$ ../configure --prefix=/usr/local/gcc-4.9.0 --with-gmp=/usr/local/gmp-6.0.0 --with-mpfr=/usr/local/mpfr-3.1.2 --with-mpc=/usr/local/mpc-1.0.2 --with-isl=/usr/local/isl-0.12.2 --with-cloog=/usr/local/cloog-0.18.1 --enable-languages=c,c++ --disable-multilib
$ make -j4

编译完成后将其安装到/usr/local/gcc-4.9.0

1
$ sudo make install

至此就完成了gcc4.9.0的编译了

1
2
$ /usr/local/gcc-4.9.0/gcc --version
gcc (GCC) 4.9.0

尝试写一段C++1y的代码测试编译,将下面的代码保存为code.cpp。

1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{

auto inc = [](auto x) { return ++x; };
std::cout << inc(1) << std::endl;
std::cout << inc('a') << std::endl;
std::cout << inc(3.4f) << std::endl;
}

编译和运行

1
2
3
4
5
$ /usr/local/gcc-4.9.0/bin/g++ -std=c++1y -o code code.cpp
$ ./code
2
b
4.4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值