> gcc --version gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0 Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
先给你的主机安装上gcc9.4版本
1
sudo apt install gcc-9 g++-9
安装之后,你应该可以通过gcc-9命令来使用9.5版本的gcc了
1 2 3 4 5
> g++-9 --version g++-9 (Ubuntu 9.5.0-1ubuntu1~22.04) 9.5.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
> sudo update-alternatives --config gcc update-alternatives: error: no alternatives for gcc
使用如下命令查看一下当前装好的gcc包,可以看到有gcc 11和9。
1 2 3 4 5 6 7 8 9 10 11
> dpkg -l | grep gcc ii gcc 4:11.2.0-1ubuntu1 amd64 GNU C compiler ii gcc-11 11.4.0-1ubuntu1~22.04 amd64 GNU C compiler ii gcc-11-base:amd64 11.4.0-1ubuntu1~22.04 amd64 GCC, the GNU Compiler Collection (base package) ii gcc-12-base:amd64 12.3.0-1ubuntu1~22.04 amd64 GCC, the GNU Compiler Collection (base package) ii gcc-9 9.5.0-1ubuntu1~22.04 amd64 GNU C compiler ii gcc-9-base:amd64 9.5.0-1ubuntu1~22.04 amd64 GCC, the GNU Compiler Collection (base package) ii lib32gcc-s1 12.3.0-1ubuntu1~22.04 amd64 GCC support library (32 bit Version) ii libgcc-11-dev:amd64 11.4.0-1ubuntu1~22.04 amd64 GCC support library (development files) ii libgcc-9-dev:amd64 9.5.0-1ubuntu1~22.04 amd64 GCC support library (development files) ii libgcc-s1:amd64 12.3.0-1ubuntu1~22.04 amd64 GCC support library
> sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 20 --slave /usr/bin/g++ g++ /usr/bin/g++-9 update-alternatives: using /usr/bin/gcc-9 to provide /usr/bin/gcc (gcc) in auto mode
使用如下命令查看可切换的gcc版本,因为当前只给工具里添加了一个gcc-9版本,所以会显示only one alternative,即只有一个可选项的意思,工具会认为没有啥好配置的。
1 2 3
> sudo update-alternatives --config gcc There is only one alternative in link group gcc (providing /usr/bin/gcc): /usr/bin/gcc-9 Nothing to configure.
Press <enter> to keep the current choice[*], or type selection number:
键入序号来选择你需要切换的gcc版本,回车即可
1 2 3 4 5
> gcc --version gcc (Ubuntu 9.5.0-1ubuntu1~22.04) 9.5.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
> g++ --version g++ (Ubuntu 9.5.0-1ubuntu1~22.04) 9.5.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
> gcov --version gcov (Ubuntu 9.5.0-1ubuntu1~22.04) 9.5.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.