毛毛桃 發表於 2020-12-18 12:24:00

CentOS 6.6 升级GCC v7.5.0 (完整)

<h2>0.为什么是GCC7.5</h2>
<p>CentOS7 自带的 gcc/g++ 版本是 4.8,如果没有安装,可以通过 yum 命令安装。</p>
<p>由于 gcc/g++ 4.8 完全支持 C++ 11,支持部分 C++ 14,不支持 C++ 17,而完全支持 C++ 17 的是 g++ 7 及以上版本:</p>
<p>个人不太建议安装GCC8.X,太新的编译器,所需要的依赖包、各种环境都需要最新版本,安装过程中会出现很多问题。而且还不一定那么稳定。</p>
<p>本着够用就好的原则,GCC7.5完全支持C++17,是最理想的选择。</p>
<h2>1.下载源码,解压</h2>
<p>&nbsp;经测试,这个&nbsp;https://ftp.gnu.org/ 地址还是很快的,而且是完整的,不想有些地方下载的是有缺文件的!!!!</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">wget</span> <span style="color: rgba(0, 128, 0, 1)">https://ftp.gnu.org/gnu/gcc/gcc-7.5.0/gcc-7.5.0.tar.xz</span>
<span style="color: rgba(0, 0, 255, 1)">tar</span> -zxf gcc-7.5.0.tar.xz</pre>
</div>
<h2>2.下载依赖</h2>
<p>我们可以直接用自带的脚本完成</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">cd</span> gcc-7.5.0
../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib</pre>
</div>
<h2>3.编译安装</h2>
<p>&nbsp;编译比较耗时,我用了七八十分钟;双核cpu, 所以 make 参数 `-j2` :</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">mkdir</span> gcc-build-7.5.0
<span style="color: rgba(0, 0, 255, 1)">cd</span> gcc-build-7.5.0
<span style="color: rgba(0, 0, 255, 1)">make</span> -j2 &amp;&amp; <span style="color: rgba(0, 0, 255, 1)">make</span> install &amp;&amp; <span style="color: rgba(0, 0, 255, 1)">date</span></pre>
</div>
<h2>4.更新动态库</h2>
<p>这个,我编译安装后,好像 不用更新,添加PATH路径 /usr/local/lib64 就可以了。</p>
<div class="cnblogs_code">
<pre># ll /usr/local/lib64/libstdc++.so*<span style="color: rgba(0, 0, 0, 1)">
lrwxrwxrwx </span><span style="color: rgba(128, 0, 128, 1)">1</span> root root       <span style="color: rgba(128, 0, 128, 1)">19</span> 12月 <span style="color: rgba(128, 0, 128, 1)">17</span> <span style="color: rgba(128, 0, 128, 1)">13</span>:<span style="color: rgba(128, 0, 128, 1)">10</span> /usr/local/lib64/libstdc++.so -&gt; libstdc++.so.<span style="color: rgba(128, 0, 128, 1)">6.0</span>.<span style="color: rgba(128, 0, 128, 1)">24</span><span style="color: rgba(0, 0, 0, 1)">
lrwxrwxrwx </span><span style="color: rgba(128, 0, 128, 1)">1</span> root root       <span style="color: rgba(128, 0, 128, 1)">19</span> 12月 <span style="color: rgba(128, 0, 128, 1)">17</span> <span style="color: rgba(128, 0, 128, 1)">13</span>:<span style="color: rgba(128, 0, 128, 1)">10</span> /usr/local/lib64/libstdc++.so.<span style="color: rgba(128, 0, 128, 1)">6</span> -&gt; libstdc++.so.<span style="color: rgba(128, 0, 128, 1)">6.0</span>.<span style="color: rgba(128, 0, 128, 1)">24</span>
-rwxr-xr-x <span style="color: rgba(128, 0, 128, 1)">1</span> root root <span style="color: rgba(128, 0, 128, 1)">11587712</span> 12月 <span style="color: rgba(128, 0, 128, 1)">17</span> <span style="color: rgba(128, 0, 128, 1)">13</span>:<span style="color: rgba(128, 0, 128, 1)">10</span> /usr/local/lib64/libstdc++.so.<span style="color: rgba(128, 0, 128, 1)">6.0</span>.<span style="color: rgba(128, 0, 128, 1)">24</span>
-rw-r--r-- <span style="color: rgba(128, 0, 128, 1)">1</span> root root   <span style="color: rgba(128, 0, 128, 1)">2397</span> 12月 <span style="color: rgba(128, 0, 128, 1)">17</span> <span style="color: rgba(128, 0, 128, 1)">13</span>:<span style="color: rgba(128, 0, 128, 1)">10</span> /usr/local/lib64/libstdc++.so.<span style="color: rgba(128, 0, 128, 1)">6.0</span>.<span style="color: rgba(128, 0, 128, 1)">24</span>-<span style="color: rgba(0, 0, 0, 1)">gdb.py
# ll /usr/lib64/libstdc++.so*<span style="color: rgba(0, 0, 0, 1)">
lrwxrwxrwx </span><span style="color: rgba(128, 0, 128, 1)">1</span> root root   <span style="color: rgba(128, 0, 128, 1)">19</span> 12月 <span style="color: rgba(128, 0, 128, 1)">16</span> <span style="color: rgba(128, 0, 128, 1)">19</span>:<span style="color: rgba(128, 0, 128, 1)">36</span> /usr/lib64/libstdc++.so.<span style="color: rgba(128, 0, 128, 1)">6</span> -&gt; libstdc++.so.<span style="color: rgba(128, 0, 128, 1)">6.0</span>.<span style="color: rgba(128, 0, 128, 1)">19</span>
-rwxr-xr-x <span style="color: rgba(128, 0, 128, 1)">1</span> root root <span style="color: rgba(128, 0, 128, 1)">995840</span> 9月<span style="color: rgba(128, 0, 128, 1)">30</span> <span style="color: rgba(128, 0, 128, 1)">10</span>:<span style="color: rgba(128, 0, 128, 1)">17</span> /usr/lib64/libstdc++.so.<span style="color: rgba(128, 0, 128, 1)">6.0</span>.<span style="color: rgba(128, 0, 128, 1)">19</span></pre>
</div>
<p>&nbsp;</p>
<p>如果程序运行报错,检查动态库:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">strings</span> /usr/lib64/libstdc++.so.<span style="color: rgba(128, 0, 128, 1)">6</span> | <span style="color: rgba(0, 0, 255, 1)">grep</span> GLIBC</pre>
</div>
<p>&nbsp;从输出可以看出,gcc的动态库还是旧版本的。说明出现这些问题,是因为升级gcc时,生成的动态库没有替换老版本gcc的动态库。</p>
<p>&nbsp;执行以下命令,查找编译gcc时生成的最新动态库:(在root权限下进行操作,后面都是,不再赘述)</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">find</span> / -xdev -name <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">libstdc++.so*</span><span style="color: rgba(128, 0, 0, 1)">"</span></pre>
</div>
<p>复制库到 /usr/lib64</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">cp</span> /usr/local/lib64/libstdc++.so.<span style="color: rgba(128, 0, 128, 1)">6.0</span>.<span style="color: rgba(128, 0, 128, 1)">24</span> /usr/lib64</pre>
</div>
<p>删掉旧的libstdc++.so.6(这是一个软链接),创建新的libstdc++.so.6软链接</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">rm</span> -rf /usr/lib64/libstdc++.so.<span style="color: rgba(128, 0, 128, 1)">6</span>
<span style="color: rgba(0, 0, 255, 1)">ln</span> -s /usr/local/lib64/libstdc++.so.<span style="color: rgba(128, 0, 128, 1)">6.0</span>.<span style="color: rgba(128, 0, 128, 1)">24</span>   /usr/lib64/libstdc++.so.<span style="color: rgba(128, 0, 128, 1)">6</span></pre>
</div>
<p>&nbsp;</p>
<h2>5.更多参考</h2>
<p>https://www.cnblogs.com/lzpong/p/5755678.html</p>
<p>https://www.cnblogs.com/FengGeBlog/p/12994103.html</p>
<p>https://www.cnblogs.com/music-liang/p/12900457.html</p>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    --- auth:lzpong<br><br>
来源:https://www.cnblogs.com/lzpong/p/14145855.html
頁: [1]
查看完整版本: CentOS 6.6 升级GCC v7.5.0 (完整)