搜索
您的当前位置:首页在 Ubuntu上安装 多个版本的GCC 、 G++ 编译器

在 Ubuntu上安装 多个版本的GCC 、 G++ 编译器

来源:乌哈旅游

       以前在Ubuntu22上编译的时候用的gcc11,现在升级到ubuntu24,发现编译不行了,版本升级到了gcc13。只好开始找解决方案。

1、安装多个版本

sudo apt update
$ sudo apt install build-essential
$ sudo apt -y install gcc-11 g++-11 gcc-13 g++-13

2、使用 update-alternatives 工具创建多个 GCC 和 G++ 编译器替代方案的列表:
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 11
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 11
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 13
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 13

  3、检查 Ubuntu 系统上可用的 C 和 C++ 编译器列表,并通过输入相关的选择编号来选择所需的版本:

$ sudo update-alternatives --config gcc
There are 3 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path            Priority   Status
------------------------------------------------------------
  0            /usr/bin/gcc-9   9         auto mode
  1            /usr/bin/gcc-10  10         manual mode
* 2            /usr/bin/gcc-8   8         manual mode
  3            /usr/bin/gcc-9   9         manual mode
Press  to keep the current choice[*], or type selection number: 

对于 C++ 编译器执行:

$ sudo update-alternatives --config g++
There are 3 choices for the alternative g++ (providing /usr/bin/g++).

  Selection    Path            Priority   Status
------------------------------------------------------------
* 0            /usr/bin/g++-9   9         auto mode
  1            /usr/bin/g++-10  10         manual mode
  2            /usr/bin/g++-8   8         manual mode
  3            /usr/bin/g++-9   9         manual mode

Press  to keep the current choice[*], or type selection number:

  4、每次切换后检查您当前选择的编译器版本:

$ gcc --version
$ g++ --version

这样就简单多了。

因篇幅问题不能全部显示,请点此查看更多更全内容

热门图文

Top