数据技术都能四世同堂,凭什么开发 30 岁就要被干掉?>>>
本文将介绍如何在 Ubuntu 12.04 下编译 gcc 4.7,我很奇怪的是在 Ubuntu 下编译 gcc 源码也那么复杂,不过 Ubuntu 也是 Linux 系统之一,就没什么好奇怪的了。
首先检查你是否已经默认安装 Ubuntu gcc 和 binutils ,如果你使用了 64 位的桌面版本,那么默认是已经安装了。下面的部分我们建议你打开一个终端来执行相应的命令。
在此之前建议更新下系统,执行如下命令:
sudo apt-get install update sudo apt-get install upgrade
接下来我们需要安装一些所需的软件包:
sudo apt-get install g++ sudo apt-get install gawk sudo apt-get install m4 sudo apt-get install gcc-multilib
然后从 GNU 网站上下载最新的 gcc-4.7 源码,请访问 http://gcc.gnu.org/mirrors.html 然后选择 gcc-4.7 的最新稳定版本,文件名是 gcc-4.7.0.tar.bz2. 将它保存到本地。
你还需要另外三个库才能成功编辑 gcc,这三个库分别是:mpc, mpfr 和 gmp. 使用链接进入下载最新版本分别是:gmp-5.0.4.tar.bz2, mpc-0.9.tar.gz 和 mpfr-3.1.0.tar.gz ,并且保存到跟 gcc 4.7 源文件所在的同一个目录。
打开终端进入上述几个文件所在的目录:
cd ~ cd Downloads
我们先开始编译 gmp 库
cd gmp* mkdir build && cd build ../configure --prefix=/usr/gcc_4_7 --build=x86_64-linux-gnu make sudo make install
几分钟后你将有一个已编译好和安装好的 gmp 库,如果一切顺利,恭喜你!
使用相同的步骤来编译 MPFR:
cd .. cd .. cd mpfr* mkdir build && cd build ../configure --build=x86_64-linux-gnu --prefix=/usr/gcc_4_7 --with-gmp=/usr/gcc_4_7 make sudo make install
紧接着是 MPC:
cd .. cd .. cd mpc* mkdir build && cd build ../configure --build=x86_64-linux-gnu --prefix=/usr/gcc_4_7 --with-gmp=/usr/gcc_4_7 --with-mpfr=/usr/gcc_4_7 make sudo make install
到这一步就完成个 gcc 所需包的编译和安装,在某些机器上这三者的编译可能超过一个小时,做好心理准备。
接下来我们可以开始编译 C/C++ 和 Fortran 编译器:
cd .. cd .. mkdir build && cd build export LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/ export C_INCLUDE_PATH=/usr/include/x86_64-linux-gnu export CPLUS_INCLUDE_PATH=/usr/include/x86_64-linux-gnu ../gcc-4.7.0/configure --build=x86_64-linux-gnu --prefix=/usr/gcc_4_7 --with-gmp=/usr/gcc_4_7 --with-mpfr=/usr/gcc_4_7 --with-mpc=/usr/gcc_4_7 --enable-checking=release --enable-languages=c,c++,fortran --disable-multilib --program-suffix=-4.7 make sudo ln -s /usr/lib/x86_64-linux-gnu /usr/lib64 sudo make install
下面命令可将 gcc 4.7 添加到系统路径中,或者直接编辑 .bashrc 文件并在最后添加下面一行:
export PATH=/usr/gcc_4_7/bin:$PATH
你不知道什么是 .bashrc 文件?没关系,打开终端执行:
cd ~ gedit .bashrc
使用下面命令重新加载 .bashrc 文件:
./.bashrc
下面是调用编译器的简单方法
g++-4.7 test.cpp -o test
如果你的磁盘空间紧张,记得删除源码文件目录下的 *build* 子目录。
接下来我们检查 g++-4.7 是否能编译一些 C++11 规范的代码,打开你喜好的编辑器,复制下面一段代码并保持该文件为 tst_lambda.cpp:
//Program to test the new C++11 lambda syntax #include <iostream> using namespace std; int main() { cout << [](int m, int n) { return m + n;} (2,4) << endl; return 0; }
编译并运行上述 lambda 示例将返回 6:
g++-4.7 -std=c++11 tst_lambda.cpp -o tst_lambda ./tst_lambda 6
g++-4.7 同时也支持 C++11 线程:
//Create a C++11 thread from the main program #include <iostream> #include <thread> //This function will be called from a thread void call_from_thread() { std::cout << "Hello, World!" << std::endl; } int main() { //Launch a thread std::thread t1(call_from_thread); //Join the thread with the main thread t1.join(); return 0; }
如果你是一个 Fortran 开发者,你可以使用 Fortran 2008 特性,例如 do concurrent
integer,parameter::mm=100000 real::a(mm), b(mm) real::fact=0.5 ! initialize the arrays ! ... do concurrent (i = 1 : mm) a(i) = a(i) + b(i) enddo end
编译并执行上述代码:
gfortran-4.7 tst_concurrent_do.f90 -o tst_concurrent_do ./tst_concurrent_do
谢谢红薯了。
这个得收藏一下。
红薯大哥,非常感觉你所写的文章。
我基于你的文章编译好了基于32位系统的gcc 4.7和
binutils 2.22
下面是我写的文章的地址:
http://www.cnblogs.com/sn-dnv-aps/archive/2012/06/08/2542205.html
过程中出现了一点问题想请教一下!
在执行
export
CPLUS_INCLUDE_PATH=/usr/include/x86_64-linux-gnu
07
../gcc-4.7.0/configure --build=x86_64-linux-gnu --prefix=/usr/gcc_4_7 --with-gmp=/usr/gcc_4_7 --with-mpfr=/usr/gcc_4_7 --with-mpc=/usr/gcc_4_7 --
enable
-checking=release --
enable
-languages=c,c++,fortran --disable-multilib --program-suffix=-4.7
的时候,出现了下面的错误:
这是什么问题啊?
万分感谢!
引用来自“徐治学”的答案
过程中出现了一点问题想请教一下!
在执行
export
CPLUS_INCLUDE_PATH=/usr/include/x86_64-linux-gnu
07
../gcc-4.7.0/configure --build=x86_64-linux-gnu --prefix=/usr/gcc_4_7 --with-gmp=/usr/gcc_4_7 --with-mpfr=/usr/gcc_4_7 --with-mpc=/usr/gcc_4_7 --
enable
-checking=release --
enable
-languages=c,c++,fortran --disable-multilib --program-suffix=-4.7
的时候,出现了下面的错误:
这是什么问题啊?
万分感谢!
我的host是ubuntu-12.04.4-server-i386.iso
编译的时候报这个错误,哪位大侠帮忙给看看
checking for C compiler default output file name... a.out
checking whether the C compiler works... configure: error: in `/download/build/x86_64-linux-gnu/libgomp':
configure: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
See `config.log' for more details.
make[2]: *** [configure-stage1-target-libgomp] Error 1
make[2]: Leaving directory `/download/build'
make[1]: *** [stage1-bubble] Error 2
make[1]: Leaving directory `/download/build'
make: *** [all] Error 2
---------------------------------------------------------------------------------------------
gcc version 4.7.0 (GCC)
configure:3644: $? = 0
configure:3633: /download/build/./gcc/xgcc -B/download/build/./gcc/ -B/usr/gcc_4_7/x86_64-linux-gnu/bin/ -B/usr/gcc_4_7/x86_64-linux-gnu/lib/ -isystem /usr/gcc_4_7/x86_64-linux-gnu/include -isystem /usr/gcc_4_7/x86_64-linux-gnu/sys-include -V >&5
xgcc: error: unrecognized command line option '-V'
xgcc: fatal error: no input files
compilation terminated.
configure:3644: $? = 1
configure:3633: /download/build/./gcc/xgcc -B/download/build/./gcc/ -B/usr/gcc_4_7/x86_64-linux-gnu/bin/ -B/usr/gcc_4_7/x86_64-linux-gnu/lib/ -isystem /usr/gcc_4_7/x86_64-linux-gnu/include -isystem /usr/gcc_4_7/x86_64-linux-gnu/sys-include -qversion >&5
xgcc: error: unrecognized command line option '-qversion'
xgcc: fatal error: no input files
compilation terminated.
configure:3644: $? = 1
configure:3664: checking for C compiler default output file name
configure:3686: /download/build/./gcc/xgcc -B/download/build/./gcc/ -B/usr/gcc_4_7/x86_64-linux-gnu/bin/ -B/usr/gcc_4_7/x86_64-linux-gnu/lib/ -isystem /usr/gcc_4_7/x86_64-linux-gnu/include -isystem /usr/gcc_4_7/x86_64-linux-gnu/sys-include -g -O2 conftest.c >&5
configure:3690: $? = 0
configure:3727: result: a.out
configure:3743: checking whether the C compiler works
configure:3752: ./a.out
/download/gcc-4.7.0/libgomp/configure: line 3754: ./a.out: cannot execute binary file
configure:3756: $? = 126
configure:3763: error: in `/download/build/x86_64-linux-gnu/libgomp':
configure:3767: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
See `config.log' for more details.
帮了大忙,万分感谢