0. 准备工作
下载如下资料并解压,默认在~/Downloads目录下:
gmp-5.0.5 (关于GMP:http://gmplib.org/)
mpfr-3.1.1 (关于MPFR:http://www.mpfr.org/mpfr-current/mpfr.html)
mpc-1.0 (关于MPC:http://www.multiprecision.org/)
gcc-4.7.1 (关于GCC:http://gcc.gnu.org/)
其中,MPFR依赖GMP,MPC建立在GMP和MPFR的基础上,而安装GCC则需要这三个库(见此)。
因此,按照如下顺序安装(为了顺利,我们先切换到root帐号:sudo -s):
1. 安装GMP
cd Downloads
cd gmp*
mkdir build && cd build
../configure --prefix=/usr/gcc-4.7.1 --enable-cxx
make
make install
优快云的编程语言连个shell脚本都没有,泪⋯⋯
cd ../../mpfr*
mkdir build && cd build
../configure --prefix=/usr/gcc-4.7.1 --with-gmp=/usr/gcc-4.7.1
make
make install
3. 安装MPC
cd ../../mpc*
mkdir build && cd build
../configure --prefix=/usr/gcc-4.7.1 --with-gmp=/usr/gcc-4.7.1 --with-mpfr=/usr/gcc-4.7.1
make
make install
4. 安装GCC
cd ../../gcc*
mkdir build && cd build
../configure --prefix=/usr/gcc-4.7.1 --enable-checking=release --with-gmp=/usr/gcc-4.7.1 --with-mpfr=/usr/gcc-4.7.1 --with-mpc=/usr/gcc-4.7.1 --program-suffix=-4.7
可以只需要特定语言,可以加个选项:
--enable-languages=c,c++,fortran
接着这一步很耗时:make
最后:make install
5. 跑通代码
由于GCC 4.7.1并没有放到环境变量里,所以我们在调用前需要设置一下环境变量:
export PATH=/usr/gcc-4.7.1/bin:$PATH
然后写段lambda代码:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
auto func = [] () { cout << "Hello, world" << endl; };
func();
return 0;
}
编译:
g++-4.7 -std=c++11 testLambda.cpp -o testLambda
执行:./testLambda 可以看到输出Hello, world。
参考:http://solarianprogrammer.com/2012/07/21/compiling-gcc-4-7-1-mac-osx-lion/