下载GCC 4.6 及其依赖源码包:
gcc-4.6.0.tar.gz
gmp-5.0.2.tar.bz2
mpfr-3.0.1.tar.bz2
mpc-0.9.tar.gz
具体下载地址可找Google问问看。
安装环境为一台装有 RHEL4.7 的PC服务器
### 主机配置信息检查
[root@Linux bin]# cat /etc/issue
Red Hat Enterprise Linux AS release 4 (Nahant Update 7)
Kernel \r on an \m
[root@Linux bin]# cat /proc/version
Linux version 2.6.9-78.ELsmp (brewbuilder@ls20-bc2-14.build.redhat.com) (gcc version 3.4.6 20060404 (Red Hat 3.4.6-10)) #1 SMP Wed Jul 9 15:46:26 EDT 2008
[root@Linux bin]# uname -r
2.6.9-78.ELsmp
[root@Linux bin]# grep "model name" /proc/cpuinfo
model name : Intel(R) Xeon(R) CPU E5504 @ 2.00GHz
model name : Intel(R) Xeon(R) CPU E5504 @ 2.00GHz
model name : Intel(R) Xeon(R) CPU E5504 @ 2.00GHz
model name : Intel(R) Xeon(R) CPU E5504 @ 2.00GHz
[root@Linux bin]# grep MemTotal /proc/meminfo
MemTotal: 16410740 kB
[root@Linux bin]# getconf LONG_BIT
64
### End Of 主机配置信息检查
cd ../gcc-toolkit/
mkdir gmp-build
mv gmp-5.0.2.tar.bz2 gmp-build/
cd gmp-build/
tar -jxvf gmp-5.0.2.tar.bz2
cd gmp-5.0.2
./configure --prefix=/usr/local/gmp-5.0.2
make
make install
tar -jxvf mpfr-3.0.1.tar.bz2
cd mpfr-3.0.1
./configure --prefix=/usr/local/mpfr-3.0.1 --with-gmp=/usr/local/gmp-5.0.2
make
make install
tar -zxvf mpc-0.9.tar.gz
cd mpc-0.9
./configure --prefix=/usr/local/mpc-0.9 --with-gmp=/usr/local/gmp-5.0.2 --with-mpfr=/usr/local/mpfr-3.0.1
make
make install
tar -zxvf gcc-4.6.0.tar.gz
cd gcc-4.6.0
./configure --prefix=/usr/local/gcc-4.6.0 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp-
5.0.2 --with-mpfr=/usr/local/mpfr-3.0.1 --with-mpc=/usr/local/mpc-0.9
注意此处环境变量一定要配置好,否则下一步make操作到一半就要后悔了,俺中午吃饭前没配这个变量,直接敲了make就去吃饭了,回来发现出错了。
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/gmp-5.0.2/lib:/usr/local/mpfr-3.0.1/lib:/usr/local/mpc-0.9/lib/
echo $LD_LIBRARY_PATH
make
此处源码编译的时间约为1个小时(我是在服务器上编译的,你如果用小本本编译就得等等了)
make install
顺利完成安装后检查一下目标服务路径中的文件及程序版本号
cd /usr/local/gcc-4.6.0/bin
[root@Linux bin]# ll
总用量 7632
-rwxr-xr-x 4 root root 955060 5月 26 13:21 c++
-rwxr-xr-x 1 root root 952539 5月 26 13:21 cpp
-rwxr-xr-x 4 root root 955060 5月 26 13:21 g++
-rwxr-xr-x 3 root root 950644 5月 26 13:21 gcc
-rwxr-xr-x 1 root root 125137 5月 26 13:21 gcov
-rwxr-xr-x 4 root root 955060 5月 26 13:21 x86_64-unknown-linux-gnu-c++
-rwxr-xr-x 4 root root 955060 5月 26 13:21 x86_64-unknown-linux-gnu-g++
-rwxr-xr-x 3 root root 950644 5月 26 13:21 x86_64-unknown-linux-gnu-gcc
-rwxr-xr-x 3 root root 950644 5月 26 13:21 x86_64-unknown-linux-gnu-gcc-4.6.0
[root@Linux i686]# gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/gcc-4.6.0/libexec/gcc/x86_64-unknown-linux-gnu/4.6.0/lto-wrapper
目标:x86_64-unknown-linux-gnu
配置为:./configure --prefix=/usr/local/gcc-4.6.0 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-
gmp=/usr/local/gmp-5.0.2 --with-mpfr=/usr/local/mpfr-3.0.1 --with-mpc=/usr/local/mpc-0.9
线程模型:posix
gcc 版本 4.6.0 (GCC)
[root@Linux i686]# g++ -v
使用内建 specs。
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/gcc-4.6.0/libexec/gcc/x86_64-unknown-linux-gnu/4.6.0/lto-wrapper
目标:x86_64-unknown-linux-gnu
配置为:./configure --prefix=/usr/local/gcc-4.6.0 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-
gmp=/usr/local/gmp-5.0.2 --with-mpfr=/usr/local/mpfr-3.0.1 --with-mpc=/usr/local/mpc-0.9
线程模型:posix
gcc 版本 4.6.0 (GCC)
环境变量配置(想换用GCC4.6为默认编译器就把这些变量配置到/etc/profile里面去)
export GCC46_HOME=/usr/local/gcc-4.6.0
export CPLUS_INCLUDE_PATH=$GCC46_HOME/include:$GCC46_HOME/include/c++/4.6.0
export LIBRARY_PATH=/usr/local/gmp-5.0.2/lib:/usr/local/mpfr-3.0.1/lib:/usr/local/mpc-0.9/lib:$GCC46_HOME/lib64:$GCC46_HOME/lib:$GCC46_HOME/lib/gcc/x86_64-
unknown-linux-gnu/4.6.0
export LD_LIBRARY_PATH=/usr/local/gmp-5.0.2/lib:/usr/local/mpfr-3.0.1/lib:/usr/local/mpc-0.9/lib:$GCC46_HOME/lib64:$GCC46_HOME/lib:
$GCC46_HOME/lib/gcc/x86_64-unknown-linux-gnu/4.6.0
export PATH=$GCC46_HOME/bin:$PATH
gcc 编译测试(一段自打印程序):
[root@Linux cpp-sourcekit]# gcc -o testc testc.c
testc.c: 在函数‘main’中:
testc.c:1:9: 警告:隐式声明与内建函数‘printf’不兼容 [enabled by default]
testc.c:1:22: 警告:赋值时将指针赋给整数,未作类型转换 [enabled by default]
testc.c:1:1: 警告:传递‘printf’的第 1 个参数时将整数赋给指针,未作类型转换 [enabled by default]
testc.c:1:1: 附注:需要类型‘const char *’,但实参的类型为‘int’
[root@Linux cpp-sourcekit]# ./testc
main(a){printf(a,34,a="main(a){printf(a,34,a=%c%s%c,34);}",34);}[root@Linux cpp-sourcekit]
g++ 编译测试:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
cout << "Yeah g++ 4.6!"<< endl;
cout << "Compiled by Michael Chen from Creaway Automation." << endl;
return 0;
}
[root@Linux cpp-sourcekit]# g++ -o test test.cpp
[root@Linux cpp-sourcekit]# ./test
Yeah g++ 4.6!
Compiled by Michael Chen from Creaway Automation.
OK, 到此顺利完成GCC4.6的安装和测试。
===========================
6月30日继续在上述环境基础之上,也就是在gcc4.6的环境中编译4.6.1的gcc源码包
将下载的gcc-4.6.1.tar.gz 源码包上传到配置好gcc-4.6.0的rhel4.7服务器,解压源码包:
tar -zxvf gcc-4.6.1.tar.gz
cd gcc-4.6.1
./configure --prefix=/usr/local/gcc-4.6.1 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp-5.0.2 --with-mpfr=/usr/local/mpfr-3.0.1 --with-mpc=/usr/local/mpc-0.9
make
---此次编译时间仍然是1小时多4分钟左右,感觉编译效率上相比之前版本的编译器没有任何提高。
make install
安装完成后,参照上次的配置修改/etc/profile文件:
vi /etc/profile
export GCC46_HOME=/usr/local/gcc-4.6.1
export CPLUS_INCLUDE_PATH=$GCC46_HOME/include:$GCC46_HOME/include/c++/4.6.1
export LIBRARY_PATH=/usr/local/gmp-5.0.2/lib:/usr/local/mpfr-3.0.1/lib:/usr/local/mpc-0.9/lib:$GCC46_HOME/lib64:$GCC46_HOME/lib:$GCC46_HOME/lib/gcc/x86_64-unknown-linux-gnu/4.6.1
export LD_LIBRARY_PATH=/usr/local/gmp-5.0.2/lib:/usr/local/mpfr-3.0.1/lib:/usr/local/mpc-0.9/lib:$GCC46_HOME/lib64:$GCC46_HOME/lib:$GCC46_HOME/lib/gcc/x86_64-unknown-linux-gnu/4.6.1
export PATH=$GCC46_HOME/bin:$PATH
exit
退出重登陆
logon
检查gcc版本号:
gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/gcc-4.6.1/libexec/gcc/x86_64-unknown-linux-gnu/4.6.1/lto-wrapper
目标:x86_64-unknown-linux-gnu
配置为:./configure --prefix=/usr/local/gcc-4.6.1 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp-5.0.2 --with-mpfr=/usr/local/mpfr-3.0.1 --with-mpc=/usr/local/mpc-0.9
线程模型:posix
gcc 版本 4.6.1 (GCC)
编译测试代码:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
char hostname[256];
gethostname(hostname, sizeof(hostname));
cout << hostname << endl;
cout << "Yeah g++ version: "<< __VERSION__ <<endl;
cout << "源文件: "<< __FILE__ << endl;
cout << "编译日期: "<< __DATE__ << endl;
cout << "编译时间: "<< __TIME__ << endl;
cout << "Compiled by Michael Chen from "<< endl << "Creaway Automation Engineering Co., Ltd. " << endl;
return 0;
}
[root@gcc gcc]# g++ -o test test.cpp
[root@gcc gcc]# ./test
gcc
Yeah g++ version: 4.6.1
源文件: test.cpp
编译日期: Jun 30 2011
编译时间: 12:16:38
Compiled by Michael Chen from
Creaway Automation Engineering Co., Ltd.
来源:oschina
链接:https://my.oschina.net/u/94915/blog/17371