都1202年了奉劝那些还在用centos6的gs,赶紧切ubuntu-Centos6 升级 gcc 惨痛教训

完整升级路线

gcc --version # 4.8.5 
yum -y update
yum -y install gcc gcc-c++ kernel-devel glibc-static libstdc++-static wget bzip2 gcc gcc-c++ glibc-headers

tar -xzvf gcc-10.2.0.tar.gz
cd gcc-10.2.0
./contrib/download_prerequisites
mkdir build
cd build
../configure -enable-checking=release -enable-languages=c,c++ -disable-multilib
 
#编译过程漫长,请耐心等待, -j4, 你几核的CPU, 就 -j 几。
make -j8
make install
 
#改变重定向
cd /usr/bin
rm -rf ./gcc
#mv gcc gcc.4.8.5
ln -s /usr/local/bin/gcc gcc
 
rm -rf ./g++
#mv g++ g++.4.8.5
ln -s /usr/local/bin/g++ g++
 
rm -rf ./c++
# mv c++ c++.4.8.5
ln -s /usr/local/bin/c++ c++
 
rm -rf ./cpp
# mv cpp cpp.4.8.5
ln -s /usr/local/bin/cpp cpp
 
cd /usr/lib/gcc/x86_64-redhat-linux
ln -s /usr/local/lib/gcc/x86_64-pc-linux-gnu/10.2.0 4.8.5
# ln -s /usr/local/lib/gcc/x86_64-pc-linux-gnu/10.2.0 4.4.7
#ln /usr/local/lib/gcc/x86_64-pc-linux-gnu/7.3.0/ 4.8.5
cd /usr/lib64
ls /usr/local/lib64/libstdc++.so.6.0.20
rm -rf ./libstdc++.so.6
ln -s /usr/local/lib64/libstdc++.so.6.0.20 libstdc++.so.6
cd /usr/local/src

wget http://ftp.gnu.org/gnu/gcc/gcc-6.3.0/gcc-6.3.0.tar.gz

tar -xzvf gcc-6.3.0.tar.gz

cd gcc-6.3.0

./contrib/download_prerequisites

mkdir build

cd build

../configure -enable-checking=release -enable-languages=c,c++ -disable-multilib

#编译过程漫长,请耐心等待, -j4, 你几核的CPU, 就 -j 几。

make -j4

make install

#改变重定向

cd /usr/bin

mv gcc gcc.4.4.7

ln -s /usr/local/bin/gcc gcc

mv g++ g++.4.4.7

ln -s /usr/local/bin/g++ g++

mv c++ c++.4.4.7

ln -s /usr/local/bin/c++ c++

mv cpp cpp.4.4.7

ln -s /usr/local/bin/cpp cpp

cd /usr/lib/gcc/x86_64-redhat-linux

ln -s /usr/local/lib/gcc/x86_64-unknown-linux-gnu/6.3.0 4.4.7

cd /usr/lib64

ls /usr/local/lib64/libstdc++.so.6.0.19

rm -rf libstdc++.so.6

ln -s /usr/local/lib64/libstdc++.so.6.0.19 libstdc++.so.6

CentOS 6 升级 GCC 带来的坑

CentOS 6 自带的 GCC 版本为 4.4.7

  1. [root@game bin]# /usr/bin/gcc -v
  2. Using built-in specs.
  3. Target: x86_64-redhat-linux
  4. Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
  5. Thread model: posix
  6. gcc version 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)

由于某些原因,我升级了 GCC 到 6.3.0 版本

  1. [root@game bin]# gcc -v
  2. Using built-in specs.
  3. COLLECT_GCC=gcc
  4. COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/6.3.0/lto-wrapper
  5. Target: x86_64-pc-linux-gnu
  6. Configured with: ../configure -enable-checking=release -enable-languages=c,c++ -disable-multilib
  7. Thread model: posix
  8. gcc version 6.3.0 (GCC)

一直以来没什么问题, 直到最近我编译安装360的配置中心组建QConf 扩展的时候,configure 正常,但是一直 make 失败。

  1. [root@game php]# /usr/local/php720/bin/phpize
  2. Configuring for:
  3. PHP Api Version: 20170718
  4. Zend Module Api No: 20170718
  5. Zend Extension Api No: 320170718
  6. [root@game php]# ./configure --with-php-config=/usr/local/php720/bin/php-config --with-libqconf-dir=/usr/local/qconf/include --enable-static LDFLAGS=/usr/local/qconf/lib/libqconf.a
  7.  
  8. ...
  9.  
  10. [root@game php]# make
  11. /bin/sh /root/soft/qconf/QConf-master/driver/php/libtool --mode=compile cc -I. -I/root/soft/qconf/QConf-master/driver/php -DPHP_ATOM_INC -I/root/soft/qconf/QConf-master/driver/php/include -I/root/soft/qconf/QConf-master/driver/php/main -I/root/soft/qconf/QConf-master/driver/php -I/usr/local/php720/include/php -I/usr/local/php720/include/php/main -I/usr/local/php720/include/php/TSRM -I/usr/local/php720/include/php/Zend -I/usr/local/php720/include/php/ext -I/usr/local/php720/include/php/ext/date/lib -I/usr/local/qconf/include -DHAVE_CONFIG_H -g -O0 -c /root/soft/qconf/QConf-master/driver/php/php_qconf.c -o php_qconf.lo
  12. mkdir .libs
  13. cc -I. -I/root/soft/qconf/QConf-master/driver/php -DPHP_ATOM_INC -I/root/soft/qconf/QConf-master/driver/php/include -I/root/soft/qconf/QConf-master/driver/php/main -I/root/soft/qconf/QConf-master/driver/php -I/usr/local/php720/include/php -I/usr/local/php720/include/php/main -I/usr/local/php720/include/php/TSRM -I/usr/local/php720/include/php/Zend -I/usr/local/php720/include/php/ext -I/usr/local/php720/include/php/ext/date/lib -I/usr/local/qconf/include -DHAVE_CONFIG_H -g -O0 -c /root/soft/qconf/QConf-master/driver/php/php_qconf.c -fPIC -DPIC -o .libs/php_qconf.o
  14. /bin/sh /root/soft/qconf/QConf-master/driver/php/libtool --mode=link cc -DPHP_ATOM_INC -I/root/soft/qconf/QConf-master/driver/php/include -I/root/soft/qconf/QConf-master/driver/php/main -I/root/soft/qconf/QConf-master/driver/php -I/usr/local/php720/include/php -I/usr/local/php720/include/php/main -I/usr/local/php720/include/php/TSRM -I/usr/local/php720/include/php/Zend -I/usr/local/php720/include/php/ext -I/usr/local/php720/include/php/ext/date/lib -I/usr/local/qconf/include -DHAVE_CONFIG_H -g -O0 /usr/local/qconf/lib/libqconf.a -o qconf.la -export-dynamic -avoid-version -prefer-pic -module -rpath /root/soft/qconf/QConf-master/driver/php/modules -lstdc++ php_qconf.lo
  15.  
  16. *** Warning: Linking the shared library qconf.la against the
  17. *** static library /usr/local/qconf/lib/libqconf.a is not portable!
  18. cc -shared .libs/php_qconf.o /usr/local/qconf/lib/libqconf.a -lstdc++ -Wl,-soname -Wl,qconf.so -o .libs/qconf.so
  19. /usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/4.4.7/libstdc++.a(functexcept.o): relocation R_X86_64_32 against `std::bad_typeid::~bad_typeid()' can not be used when making a shared object; recompile with -fPIC
  20. /usr/lib/gcc/x86_64-redhat-linux/4.4.7/libstdc++.a: could not read symbols: Bad value
  21. collect2: ld returned 1 exit status
  22. make: *** [qconf.la] Error 1

百度、Google一直没有找到原因,后来从上图错误分析发现,/usr/lib/gcc/x86_64-redhat-linux/4.4.7/libstdc++.a: could not read symbols: Bad value  ,GCC已经升级到 6.3.0 了,但是静态库还是用的4.4.7, 然后我找到 /usr/lib/gcc/x86_64-redhat-linux/4.4.7 目录,将 4.4.7 目录备份,并找到 6.3.0 的库  /usr/local/lib/gcc/x86_64-pc-linux-gnu/6.3.0/ , 将4.4.7目录重定向到该目录,完美解决。

  1. [root@game x86_64-redhat-linux]# cd /usr/lib/gcc/x86_64-redhat-linux
  2. [root@game x86_64-redhat-linux]# ln -s /usr/local/lib/gcc/x86_64-pc-linux-gnu/6.3.0 4.4.7

然后 make 就好了

  1. [root@game php]# make
  2. /bin/sh /root/soft/qconf/QConf-master/driver/php/libtool --mode=link cc -DPHP_ATOM_INC -I/root/soft/qconf/QConf-master/driver/php/include -I/root/soft/qconf/QConf-master/driver/php/main -I/root/soft/qconf/QConf-master/driver/php -I/usr/local/php720/include/php -I/usr/local/php720/include/php/main -I/usr/local/php720/include/php/TSRM -I/usr/local/php720/include/php/Zend -I/usr/local/php720/include/php/ext -I/usr/local/php720/include/php/ext/date/lib -I/usr/local/qconf/include -DHAVE_CONFIG_H -g -O0 /usr/local/qconf/lib/libqconf.a -o qconf.la -export-dynamic -avoid-version -prefer-pic -module -rpath /root/soft/qconf/QConf-master/driver/php/modules -lstdc++ php_qconf.lo
  3.  
  4. *** Warning: Linking the shared library qconf.la against the
  5. *** static library /usr/local/qconf/lib/libqconf.a is not portable!
  6. cc -shared .libs/php_qconf.o /usr/local/qconf/lib/libqconf.a -lstdc++ -Wl,-soname -Wl,qconf.so -o .libs/qconf.so
  7. creating qconf.la
  8. (cd .libs && rm -f qconf.la && ln -s ../qconf.la qconf.la)
  9. /bin/sh /root/soft/qconf/QConf-master/driver/php/libtool --mode=install cp ./qconf.la /root/soft/qconf/QConf-master/driver/php/modules
  10. cp ./.libs/qconf.so /root/soft/qconf/QConf-master/driver/php/modules/qconf.so
  11. cp ./.libs/qconf.lai /root/soft/qconf/QConf-master/driver/php/modules/qconf.la
  12. PATH="$PATH:/sbin" ldconfig -n /root/soft/qconf/QConf-master/driver/php/modules
  13. ----------------------------------------------------------------------
  14. Libraries have been installed in:
  15. /root/soft/qconf/QConf-master/driver/php/modules
  16.  
  17. If you ever happen to want to link against installed libraries
  18. in a given directory, LIBDIR, you must either use libtool, and
  19. specify the full pathname of the library, or use the `-LLIBDIR'
  20. flag during linking and do at least one of the following:
  21. - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
  22. during execution
  23. - add LIBDIR to the `LD_RUN_PATH' environment variable
  24. during linking
  25. - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
  26. - have your system administrator add LIBDIR to `/etc/ld.so.conf'
  27.  
  28. See any operating system documentation about shared libraries for
  29. more information, such as the ld(1) and ld.so(8) manual pages.
  30. ----------------------------------------------------------------------
  31.  
  32. Build complete.
  33. Don't forget to run 'make test'.

    CentOS 6.6 升级GCC G++ (当前最新GCC/G++版本为v6.1.0)

    没有便捷方式,

    yum update....   yum install 

    或者 添加yum 的 repo 文件 也不行, 只能更新到 4.4.7!

    then,  只能手动编译安装了,那么开始第一步下载源代码吧,GO!

    1、 获取安装包并解压

    wget http://ftp.gnu.org/gnu/gcc/gcc-6.1.0/gcc-6.1.0.tar.bz2

    tar -jxvf gcc-6.1.0.tar.bz2

    当然,http://ftp.gnu.org/gnu/gcc  里面有所有的gcc版本供下载,最新版本已经有6.1.0啦.

    建议下载.bz2的压缩包,文件更小,下载时间更少.

    2、 下载供编译需求的依赖项

    参考文献[1]中说:这个神奇的脚本文件会帮我们下载、配置、安装依赖库,可以节约我们大量的时间和精力。

    cd gcc-6.1.0

    ./contrib/download_prerequisites 

    3、 建立一个目录供编译出的文件存放

    mkdir gcc-build-6.1.0

    cd gcc-build-6.1.0

    4、 生成Makefile文件

    ../configure -enable-checking=release -enable-languages=c,c++ -disable-multilib

    5、 编译

    make -j4

    -j4选项是make对多核处理器的优化,如果不成功请使用 make,相关优化选项可以移步至参考文献[2]。

    (注意:此步骤非常耗时,我虚拟机耗时近3小时; 实体机近80分钟,CPU基本是满的,内存也使用不少)

    6、 安装

    make install

    (安装需要root权限!)

    查看安装

    ls /usr/local/bin | grep gcc

    7、 重启,然后查看gcc版本

    gcc -v

    8、 写个C++11 特性的程序段 测试

    tryCpp11.cc 代码省略....

    g++ -std=c++11 -o tryCpp11 tryCpp11.cc

    9、升级gcc,生成的动态库没有替换老版本gcc的动态库

    源码编译升级安装了gcc后,编译程序或运行其它程序时,有时会出现类似/usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found的问题。这是因为升级gcc时,生成的动态库没有替换老版本gcc的动态库导致的,将gcc最新版本的动态库替换系统中老版本的动态库即可解决。

    9.1 运行以下命令检查动态库:

    strings /usr/lib64/libstdc++.so.6 | grep GLIBC

    从输出可以看出,gcc的动态库还是旧版本的。说明出现这些问题,是因为升级gcc时,生成的动态库没有替换老版本gcc的动态库。

    9.2 执行以下命令,查找编译gcc时生成的最新动态库:

    find / -name "libstdc++.so*"

    将上面的最新动态库libstdc++.so.6.0.22复制到/usr/lib64目录下

    cd /usr/lib64

    cp /root/Downloads/gcc-6.1.0/gcc-build-6.1.0/stage1-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.22 ./

    9.3 删除原来软连接:

    rm -rf libstdc++.so.6

    9.4 将默认库的软连接指向最新动态库:

    ln -s libstdc++.so.6.0.22 libstdc++.so.6

    9.5 默认动态库升级完成。重新运行以下命令检查动态库:

    strings /usr/lib64/libstdc++.so.6 | grep GLIBC

    可以看到 输出有"GLIBCXX_3.4.21" 了

     

=========================

工作机会(内部推荐):发送邮件至gaoyabing@126.com,看到会帮转内部HR。

邮件标题:X姓名X_X公司X_简历(如:张三_东方财富_简历),否则一律垃圾邮件!

公司信息:

  1. 1.东方财富|上海徐汇、南京|微信客户端查看职位(可自助提交信息,微信打开);
原文地址:https://www.cnblogs.com/Chary/p/14373369.html