博客地址:http://home.cnblogs.com/u/zengjianrong/

ubuntu升级gcc/g++版本

1. 确认待升级的版本是否存在,若存在,则跳转到第4步

sudo apt-cache search gcc-9

2. 不存在则需要添加 ubuntu toolchain的test源

sudo add-apt-repository ppa:ubuntu-toolchain-r/test

3. 由于默认源在国内速度太慢,建议修改到ustc

# 备份
sudo cp /etc/apt/sources.list.d/ubuntu-toolchain-r-ubuntu-test-bionic.list /etc/apt/sources.list.d/ubuntu-toolchain-r-ubuntu-test-bionic.list.zjr

# 修改
cat /etc/apt/sources.list.d/ubuntu-toolchain-r-ubuntu-test-bionic.list
#deb http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic main
# deb-src http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic main
deb https://launchpad.proxy.ustclug.org/ubuntu-toolchain-r/test/ubuntu bionic main
# deb-src https://launchpad.proxy.ustclug.org/ubuntu-toolchain-r/test/ubuntu bionic main

4. 升级

sudo apt update
sudo apt install gcc-9 g++-9

5. 配置默认gcc/g++ 链接优先级,最后的数字越大优先级越高

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 20
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 20

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 10

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 1
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 1

6. 测试

gcc --version

g++ --version

 

posted @ 2022-08-25 14:50  black_man  阅读(4712)  评论(0编辑  收藏  举报