build gcc-4.8.5 on debian7.5

前言

有个同事离职了, 编译环境在他的虚拟机里面, 因为离职的原因太不堪了, 没有联系他的必要性.
平时要求每个研发做任务(无论事情的大小)之后,整理,提供,归档文档的重要性就突显重要。
没有文档,也不是说有多难的事,只是剩下的人就需要自己重新开始整个过程,浪费时间而已.
要是技术含量较高的任务,如果没文档,那重头开始一个任务的时间就更长了。

这个编译任务是在debian7.5上编译带c++11语法的c++程序. 手头只有debian8.8能编译c++11的环境.
在debian8.8上编译好的程序输出,在debian7.5上是无法运行的, 依赖的库不一样.
在原版的debian7.5上编译带c++11语法的c++程序,语法不认识.
需要编译一个支持c++11的gcc换上,然后再编译目标程序。

试验目标

编译一个支持c++11的gcc, 替换debian7.5上原版的gcc4.4或gcc4.7.
查资料可知, gcc4.8.x是支持c++11的。gcc4.8.x最后一个版本是gcc4.8.5.

试验材料

gcc_4.8.5_stuff.7z

试验

gcc各版本的下载点

https://gcc.gnu.org/releases.html
下载得到 gcc-4.8.5.tar.bz2

gcc的官方编译手册

http://gcc.gnu.org/install/

编译过程中用到的参考资料

https://stackoverflow.com/questions/448457/how-to-use-multiple-versions-of-gcc
https://stackoverflow.com/questions/2969222/make-gnu-make-use-a-different-compiler
https://www.linuxquestions.org/questions/programming-9/making-make-use-a-specific-gcc-version-359296/

编译过程

解压源码包

mkdir -p /home/dev/
// cp gcc-4.8.5.tar.bz2 to /home/dev/
cd /home/dev/
bzip2 -d gcc-4.8.5.tar.bz2
tar -xvf gcc-4.8.5.tar
cd gcc-4.8.5

下载编译依赖项

./contrib/download_prerequisites

建立编译用的临时目录

cd ..

mkdir /home/dev/tmp_dir
mkdir /home/dev/gcc_4.8.5_install

cd /home/dev/tmp_dir

配置工程选项

/home/dev/gcc-4.8.5/configure –prefix=/home/dev/gcc_4.8.5_install –disable-multilib

// –disable-multilib 选项非常必要, 只编译当前平台的gcc版本, 节省编译时间. 防止编译其他平台gcc时,报各种奇怪的错误.

开始编译

make

解决编译报错

编译的时间挺长的,几个小时. 虚拟机给的配置还可以的(memory 2GB + cpu 2x2)
// configure: error: cannot find neither zip nor jar, cannot continue
aptitude install gcc-multilib
aptitude install zip

继续编译

make
编译成功了

安装编译好的新版gcc

make install
安装后有一段提示备用

----------------------------------------------------------------------
Libraries have been installed in:
   /home/dev/gcc_4.8.5_install/lib/../lib64

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

用编译好的gcc替换系统中旧版的gcc

查看原版gcc的位置

root@debian750devmin:/home/dev/tmp_dir# find / -name 'gcc'
/home/dev/gcc-4.8.5/gcc
/home/dev/gcc-4.8.5/gcc/testsuite/ada/acats/tests/gcc
/home/dev/gcc_4.8.5_install/lib/gcc
/home/dev/gcc_4.8.5_install/bin/gcc
/home/dev/gcc_4.8.5_install/libexec/gcc
/home/dev/tmp_dir/gcc
/usr/lib/gcc
/usr/bin/gcc
/usr/share/doc/gcc
/usr/share/doc/gcc-4.7-base/gcc
/usr/share/bash-completion/completions/gcc
root@debian750devmin:~# whereis gcc
gcc: /usr/bin/gcc /usr/lib/gcc /usr/bin/X11/gcc
root@debian750devmin:~# ls -l /usr/bin/gcc
lrwxrwxrwx 1 root root 7  927  2012 /usr/bin/gcc -> gcc-4.7*

查看原版g++的位置

root@debian750devmin:~# find / -name 'g++'
/home/dev/gcc_4.8.5_install/bin/g++
/usr/bin/g++
/usr/share/doc/g++
/usr/share/bash-completion/completions/g++
root@debian750devmin:~# whereis g++
g++: /usr/bin/g++ /usr/bin/X11/g++
root@debian750devmin:~# ls /usr/bin/g++
lrwxrwxrwx 1 root root 7  927  2012 /usr/bin/g++ -> g++-4.7*

替换新版gcc

rm -f /usr/bin/gcc
ln -s /home/dev/gcc_4.8.5_install/bin/gcc /usr/bin/gcc

替换新版g++

rm -f /usr/bin/g++
ln -s /home/dev/gcc_4.8.5_install/bin/g++ /usr/bin/g++

查看当前gcc版本

root@debian750devmin:~# gcc --version | head -n1
gcc (GCC) 4.8.5

查看当前g++版本

root@debian750devmin:~# g++ --version | head -n1
g++ (GCC) 4.8.5

找个测试工程,验证一个新版编译

可以,编译过了.
现在c++11语法支持了,正式工程还缺其他库, 另外的试验了.

猜你喜欢

转载自blog.csdn.net/lostspeed/article/details/80631397
今日推荐