为何要安装其他版本的gcc
总有很多时候,Ubuntu默认的高版本gcc不符合现在项目的需求,需要我们用稍低版本的gcc进行编译。这个时候,我们就需要学着如何安装稍低版本的gcc,在两者并存才情况下,指定稍低版本进行工作。
如何安装
1.下载gcc安装包,比如这里我是用的gcc-4.1.2版本为例。
2.软链接:
zzh@ubuntu14:/usr/include$ sudo ln -s x86_64-linux-gnu/bits bits
zzh@ubuntu14:/usr/include$ sudo ln -s x86_64-linux-gnu/gnu gnu
zzh@ubuntu14:/usr/include$ sudo ln -s x86_64-linux-gnu/sys sys
zzh@ubuntu14:/usr/include$ sudo ln -s x86_64-linux-gnu/asm asm
zzh@ubuntu14:/usr/include$ cd /usr/lib/
zzh@ubuntu14:/usr/lib$ sudo ln -s x86_64-linux-gnu/crt
crt1.o crti.o crtn.o
zzh@ubuntu14:/usr/lib$ sudo ln -s x86_64-linux-gnu/crt1.o crt1.o
zzh@ubuntu14:/usr/lib$ sudo ln -s x86_64-linux-gnu/crti.o crti.o
zzh@ubuntu14:/usr/lib$ sudo ln -s x86_64-linux-gnu/crtn.o crtn.o
3.进入gcc目录下,新建一个build文件夹,在这里面进行配置编译安装。
zzh@ubuntu14:/usr/lib$ cd ~/gcc-4.1.2/
zzh@ubuntu14:~/gcc-4.1.2$ mkdir build
zzh@ubuntu14:~/gcc-4.1.2$ cd build
zzh@ubuntu14:~/gcc-4.1.2/build$ ../configure --prefix=/usr/local/ --program-prefix=sse- --libexecdir=/usr/local/lib --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --disable-multilib --enable-languages=c,c++
zzh@ubuntu14:~/gcc-4.1.2/build$ make bootstrap
zzh@ubuntu14:~/gcc-4.1.2/build$ sudo make install
4.进入bin目录下,查看gcc是否安装好
zzh@ubuntu14:~/gcc-4.1.2/build$ cd /usr/local/bin
zzh@ubuntu14:/usr/local/bin$ ll *gcc*
-rwxr-xr-x 3 root root 476075 3月 16 10:08 sse-gcc*
-rwxr-xr-x 1 root root 16041 3月 16 10:08 sse-gccbug*
-rwxr-xr-x 3 root root 476075 3月 16 10:08 x86_64-unknown-linux-gnu-gcc-4.1.2*
-rwxr-xr-x 3 root root 476075 3月 16 10:08 x86_64-unknown-linux-gnu-sse-gcc*
看到上述的内容就是已经安装好啦
5.接下来,我们可以修改gcc的软链接,直接指向我们刚刚安装的低版本。
zzh@ubuntu14:~/gcc-4.1.2/build$ cd /usr/bin
//要先删掉已有的软链接
zzh@ubuntu14:/usr/bin$ sudo rm gcc
zzh@ubuntu14:/usr/bin$ sudo ln -s /usr/local/bin/x86_64-unknown-linux-gnu-gcc-4.1.2 gcc
这样当我们查看gcc版本的时候,就会显示是我们的低版本了。
zzh@ubuntu14:/usr/bin$ gcc --version
gcc (GCC) 4.1.2
6.同理,g++的版本也是可以改的
zzh@ubuntu14:/usr/bin$ sudo rm g++
zzh@ubuntu14:/usr/bin$ sudo ln -s /usr/local/bin/x86_64-unknown-linux-gnu-sse-g++ g++
zzh@ubuntu14:/usr/bin$ g++ -v
Using built-in specs.
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --prefix=/usr/local/ --program-prefix=sse- --libexecdir=/usr/local/lib --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --disable-multilib --enable-languages=c,c++
Thread model: posix
gcc version 4.1.2
7.又想改回来之前的高版本,也是一样的道理哒~~
zzh@ubuntu14:/usr/bin$ sudo rm g++
zzh@ubuntu14:/usr/bin$ sudo ln -s g++-4.8 g++
zzh@ubuntu14:/usr/bin$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.4-2ubuntu1~14.04.3' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)