本文记录了在Linux系统上安装多版本gcc/g++的过程。
GNU编译器(GCC, GNU Compiler Collection)是GNU工具链的关键组件,与GNU、Linux相关项目的标准编译器。它设计之初仅用来处理C语言的(也被称为GNU C编译器),紧接着扩展到C++、Objective-C/C++、Fortran、Java、Go等编程语言。
如果你的系统没有安装GNU编译器,那么需要执行如下命令安装基础开发工具包,该命令安装了gcc、g++、make等一系列基础工具包
sudo apt update
sudo apt install build-essential
查看安装gcc、g++版本。安装成功后使用gcc --version
和g++ --version
命令查看所安装的gcc版本。
至此GNU编译器安装完成
首先查看当前系统已安装的编译器版本
ls /usr/bin/g++*
ls /usr/bin/gcc*
笔者的需求是安装gcc-9和gcc-10,因此执行以下命令安装对应版本的编译器
sudo apt install gcc-9 g++-9 gcc-10 g++-10
出现以下提示即为安装成功
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
pastebinit python3-newt run-one
Use 'sudo apt autoremove' to remove them.
Suggested packages:
g++-10-multilib gcc-10-doc libstdc++6-10-dbg g++-9-multilib gcc-9-doc libstdc++6-9-dbg
gcc-10-multilib gcc-10-locales libgcc-s1-dbg libgomp1-dbg libitm1-`在这里插入代码片`dbg libatomic1-dbg
libasan6-dbg liblsan0-dbg libtsan0-dbg libubsan1-dbg libquadmath0-dbg gcc-9-multilib
gcc-9-locales libgcc1-dbg libasan5-dbg
The following NEW packages will be installed:
g++-10 g++-9 gcc-10 gcc-9
0 upgraded, 4 newly installed, 0 to remove and 10 not upgraded.
Need to get 42.1 MB of archives.
After this operation, 150 MB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 gcc-10 amd64 10.3.0-1ubuntu1~18.04~1 [16.4 MB]
Get:2 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 g++-10 amd64 10.3.0-1ubuntu1~18.04~1 [9,085 kB]
Get:3 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 gcc-9 amd64 9.4.0-1ubuntu1~18.04 [8,257 kB]
Get:4 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 g++-9 amd64 9.4.0-1ubuntu1~18.04 [8,399 kB]
Fetched 42.1 MB in 19min 34s (35.9 kB/s)
Selecting previously unselected package gcc-10.
(Reading database ... 249235 files and directories currently installed.)
Preparing to unpack .../gcc-10_10.3.0-1ubuntu1~18.04~1_amd64.deb ...
Unpacking gcc-10 (10.3.0-1ubuntu1~18.04~1) ...
Selecting previously unselected package g++-10.
Preparing to unpack .../g++-10_10.3.0-1ubuntu1~18.04~1_amd64.deb ...
Unpacking g++-10 (10.3.0-1ubuntu1~18.04~1) ...
Selecting previously unselected package gcc-9.
Preparing to unpack .../gcc-9_9.4.0-1ubuntu1~18.04_amd64.deb ...
Unpacking gcc-9 (9.4.0-1ubuntu1~18.04) ...
Selecting previously unselected package g++-9.
Preparing to unpack .../g++-9_9.4.0-1ubuntu1~18.04_amd64.deb ...
Unpacking g++-9 (9.4.0-1ubuntu1~18.04) ...
Setting up gcc-9 (9.4.0-1ubuntu1~18.04) ...
Setting up g++-9 (9.4.0-1ubuntu1~18.04) ...
Setting up gcc-10 (10.3.0-1ubuntu1~18.04~1) ...
Setting up g++-10 (10.3.0-1ubuntu1~18.04~1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
为每个版本配置替代版本,并将优先级与之关联,系统默认版本为优先级最高的版本。
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 90
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 100
如果你之后不满意优先级的设置,需要切换gcc/g++版本,可以使用如下命令切换
切换gcc版本
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-10 100 auto mode
1 /usr/bin/gcc-10 100 manual mode
2 /usr/bin/gcc-7 70 manual mode
3 /usr/bin/gcc-9 90 manual mode
Press <enter> to keep the current choice[*], or type selection number:
✳代表当前正在使用的版本,输入Selection
一列的数字切换版本。
这里我们输入3+回车切换gcc版本。
与gcc同理,命令sudo update-alternatives --config g++
切换g++版本。
以上就是今天要分享的内容,本文介绍了如何在Ubuntu18.04系统下,安装与切换多版本gcc/g++。
如果本文能给你带来帮助的话,点个赞鼓励一下作者吧!