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

摘要:
  • 介绍CentOS系统下安装G++6.1环境
  • 补充说明(用多线程程序来验证)

 
安装g++,在root 权限下,执行下面的命令
  yum install gcc-c++
CentOS 6.6 升级GCC G++ (当前最新GCC/G++版本为v6.1.0)
没有便捷方式,
yum update....   yum install
或者 添加yum 的 repo 文件 也不行, 只能更新到 4.4.7!
then,  只能手动编译安装了,那么开始第一步下载源代码吧,GO! 

 
 
1、 获取安装包并解压
 
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*"
 
在centos7下面,需要添加  -xdev 过滤
find  /  -xdev  -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" 了
 
 
[注:摘抄自http://www.linuxidc.com/Linux/2015-01/112595.htm,  http://itbilu.com/linux/management/NymXRUieg.html, 原作者编译有出错过,但是我编译没有出错过]
 
 
 
 
 
 
!!!且慢!!!:
添加环境变量
 
Now put the gcc in path
export PATH=/usr/local/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH

请参考  https://www.cnblogs.com/music-liang/p/11889602.html    这篇文章,将环境变量永久添加到路径中。

 
 
 
 
 
G++升级之后,lib64快捷方式修改
 
1.安装g++                                            
2.更新到最新版本g++                                    
3.修改 lib64文件夹下面某个库文件的链接                         
4.eg: g++  Demo.cpp  -o Demo.bin -std=c++11  -pthread         
编译Demo.cpp文件,生成 Demo.bin文件, 用的C++11最新编程,如果有线程,还需要 -pthread 选项     
 
配置好本地  lib64文件夹下面的链接!!!(否则运行会报错、提示某个版本的库文件不是最新版本)
----
updatedb
-----查看
locate libstdc++.so.6
rm /usr/lib64/libstdc++.so.6
ln -s /usr/local/lib64/libstdc++.so.6.0.22  /usr/lib64/libstdc++.so.6
 
///////测试C++11特性的2个Demo
 
 
#include <iostream>
#include <thread>
 
void hello()
{
    std::cout<<"Hello Concurrent World
";
}
 
int main()
{
    std::thread t(hello);
    t.join();
}
 
#include <iostream>
#include <vector>
#include <unistd.h>
using namespace std;
typedef unsigned char       BYTE;
 
int main()
{
vector<int> int_vec;
int_vec.push_back(1);
int_vec.push_back(2);
int_vec.push_back(3);
int_vec.push_back(4);
 
for(auto v : int_vec)
{
cout << v << "----hello C++11. -std=c++11 "<< getpid() << endl;
}
 
    BYTE  bt1 = 0x81;
    BYTE  bt2 = 0xEE;
 
    int itest1 = 0x01;
    int itest2 = 0xFF;
    int itest3 = bt1;
    int itest4 = bt2;
    cout << "itest1=" << itest1 << ", itest2=" << itest2 << ", itest3=" << itest3 << ", itest4=" << itest4 << endl;
 
 
return 0;
}
 
 
 
 
////////////  g++ chap1-1.cpp -std=c++11 -pthread -o abc.bin
 
 
#include <iostream>
#include <thread>
#include <unistd.h>
#include <vector>
using namespace std;
 
void MyThread(int iPara)
{
    cout<<"MyThread----: "<< iPara <<endl;
    sleep(1); ///The para is Seconds
}
 
int main()
{
    thread  threadDemo(MyThread, 123);
    cout <<"-----------start"<<endl;
    threadDemo.join();
    cout <<"-----------end"<<endl;
    
    vector<int> vecTest;
    vecTest.clear();
    vecTest.push_back(10);
    vecTest.push_back(20);
    vecTest.push_back(30);
    vecTest.push_back(40);
    vecTest.push_back(50);
    vecTest.push_back(1);
    vecTest.push_back(2);   
    for(auto  &changeValue:vecTest)
    {
        changeValue = changeValue * 2;
    }
    ////////////
    //
    cout<<"display new value:"<<endl;
    for(auto newValue:vecTest)
    {
        cout << newValue<<endl;
    }
    return 0;
}
 
 
 
 
原文地址:https://www.cnblogs.com/music-liang/p/11908455.html