下載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.