1. 下载并解压源码:
wget http://mirrors.ustc.edu.cn/gnu/gcc/gcc-4.1.2/gcc-4.1.2.tar.bz2
tar jxvf gcc-4.1.2.tar.bz2
2. 安装依赖库及软件:
sudo apt-get install libc6-dev libgmp-dev libmpfr-dev texinfo (这一步可以省去)
3. 编译gcc:
cd gcc-4.1.2
mkdir build
cd build
../configure --prefix=/opt/gcc-4.1.2 --program-suffix=-4.1 --libexecdir=/opt/gcc-4.1.2/lib --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --disable-multilib --enable-languages=c,c++
make bootstrap
然后是漫长的等待。。。
4. 安装gcc:
make install
cd /opt/gcc-4.1.2/bin
ls -al
看到i686-pc-linux-gnu-gcc-4.1.2等文件就说明安装完成啦。
5. 切换gcc 4.1.2:
我一般喜欢在主目录下建立一个bin文件夹,而bin文件夹默认就在PATH中,然后在bin文件夹下链接gcc-4.1来切换gcc的不同版本。
cd ~
mkdir bin
ln -s /opt/gcc-4.1.2/bin/gcc-4.1 gcc
ln -s /opt/gcc-4.1.2/bin/g++-4.1 g++
ln -s /opt/gcc-4.1.2/bin/cpp-4.1 cpp
ln -s /opt/gcc-4.1.2/bin/gcov-4.1 gcov
ln -s gcc cc
ln -s g++ c++
现在重新登录,输入gcc -v 或 cc -v就可以显示gcc已经变成4.1.2版本啦。
注意编译时遇到的问题小结:
(1)makeinfo的问题
# ./configure
接着就出现错误
WARNING: `makeinfo' is missing on your system. You should only need it if you modified a `.texi' or `.texinfo' file, or any other file indirectly affecting the aspect of the manual. The spurious call might also be the consequence of using a buggy `make' (AIX, DU, IRIX). You might want to install the `Texinfo' package or the `GNU make' package. Grab either from any GNU archive site. make[3]: *** [fastjar.info] 错误 1 make[3]:正在离开目录 `/home/ssm/gcc-obj/fastjar' make[2]: *** [all] 错误 2 make[2]:正在离开目录 `/home/ssm/gcc-obj/fastjar' make[1]: *** [all-fastjar] 错误 2 make[1]:正在离开目录 `/home/ssm/gcc-obj' make: *** [all] 错误 2