GCC版本升级——基于源码编译

GCC版本升级——基于源码编译

因ubuntu版本比较旧,而项目需要更高版本的gcc,故需升级。

这里记录源码升级gcc的过程。

由于直接下载gcc源码后编译会出现依赖报错,这里直接先记录编译依赖的过程,具体有以下三个依赖:

  • gmp
  • mpfr
  • mpc

笔者的编译目录为/home/ubuntu/gcc,故编译指令中的路径,请读者自行替换。

先下载源码:

wget https://gcc.gnu.org/pub/gcc/infrastructure/mpfr-4.1.0.tar.bz2 --no-check-certificate
wget https://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.2.1.tar.bz2 --no-check-certificate
wget https://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.2.1.tar.gz --no-check-certificate

依次编译各依赖。
gmp:

tar -xvjf gmp-6.2.1.tar.bz2
cd gmp-6.2.1 && ./configure --prefix=$(pwd)
make
sudo make install

mpfr:

tar -xvjf mpfr-4.1.0.tar.bz2
cd mpfr-4.1.0/

./configure --prefix=$(pwd) \
    --with-gmp-include=/home/ubuntu/gcc/gmp-6.2.1/include \
    --with-gmp-lib=/home/ubuntu/gcc/gmp-6.2.1/lib \

make
sudo make install

mpc:

tar -xvzf mpc-1.2.1.tar.gz
cd mpc-1.2.1/

./configure --prefix=$(pwd) \
    --with-gmp-include=/home/ubuntu/gcc/gmp-6.2.1/include \
    --with-gmp-lib=/home/ubuntu/gcc/gmp-6.2.1/lib \
    --with-mpfr-include=/home/ubuntu/gcc/mpfr-4.1.0/include \
    --with-mpfr-lib=/home/ubuntu/gcc/mpfr-4.1.0/lib \

make
sudo make install

修改环境变量:

export LD_LIBRARY_PATH=/home/ubuntu/gcc/gmp-6.2.1/lib:/home/ubuntu/gcc/mpfr-4.1.0/lib:/home/ubuntu/gcc/mpc-1.2.1/lib:$LD_LIBRARY_PATH

至此,依赖编译完成,可以编译gcc了。
从gcc官网选择对应的版本,并下载源码:

<�a href="https://ftp.gnu.org/gnu/gcc/">Index of /gnu/gcc<�/a>

这里选用的版本是8.5.0

tar -xvzf gcc-8.5.0.tar.gz
cd gcc-8.5.0

./configure \
    --disable-multilib \
    --prefix=$(pwd)/ \
    --with-gmp-include=/home/ubuntu/gcc/gmp-6.2.1/include \
    --with-gmp-lib=/home/ubuntu/gcc/gmp-6.2.1/lib \
    --with-mpfr-include=/home/ubuntu/gcc/mpfr-4.1.0/include \
    --with-mpfr-lib=/home/ubuntu/gcc/mpfr-4.1.0/lib \
    --with-mpc-include=/home/ubuntu/gcc/mpc-1.2.1/include \
    --with-mpc-lib=/home/ubuntu/gcc/mpc-1.2.1/lib \

make -j8
sudo make install

注意:make耗时比较久。

修改环境变量:

export PATH=/home/ubuntu/gcc/gcc-8.5.0/bin:$PATH
export LIBRARY_PATH=/home/ubuntu/gcc/gcc-8.5.0/lib64:$LIBRARY_PATH
export LD_LIBRARY_PATH=/home/ubuntu/gcc/gcc-8.5.0/lib64:$LD_LIBRARY_PATH
export C_INCLUDE_PATH=/home/ubuntu/gcc/gcc-8.5.0/include:$C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH=/home/ubuntu/gcc/gcc-8.5.0/include:$CPLUS_INCLUDE_PATH

最后用新的gcc替换旧的gcc。

先找到旧的:

which gcc

具体为:

/usr/bin/gcc

删除:

sudo rm /usr/bin/gcc
sudo rm /usr/bin/g++

创建新的链接,指向新的gcc文件:

sudo ln -s /home/ubuntu/gcc/gcc-8.5.0/bin/gcc /usr/bin/gcc
sudo ln -s /home/ubuntu/gcc/gcc-8.5.0/bin/g++ /usr/bin/g++

检查gcc版本:

gcc --version
posted @   付时凡  阅读(269)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示