Ubuntu gcc/g++默认版本切换

Ubuntu中gcc/g++默认版本切换

ubuntu系统中可能安装了多个版本的gcc/g++ 如gcc-5,gcc-7, gcc-9等等,在使用时,我们可以在命令直接指定gcc/g++版本来确定使用哪个编译器

1
2
g++-5 ./test.cpp -o test
g++-7 ./test.cpp -o test

此外,我们还可以通过包管理工具update-alternatives来设置默认gcc/g++版本

1
2
3
4
5
6
7
8
9
10
11
12
13
# 设置gcc优先级,最后的数字代表优先程度,数值越大,优先程度越高,可自行调整各版本的优先级
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 50
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90
# 设置g++优先级
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 50
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 70
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 90

# 查看gcc各版本优先级
sudo update-alternatives --config gcc
# 查看g++各版本优先级
sudo update-alternatives --config g++

如上的命令后,gcc-9和g++-9的优先级设为最高,默认使用该版本,如需检查默认gcc/g++版本,使用如下命令

1
2
gcc -v
g++ -v

Ubuntu gcc/g++默认版本切换
https://izhuhaoran.github.io/2023/01/23/Linux_Note/gcc默认版本切换/
作者
zhuhr
发布于
2023年1月23日
许可协议