在 Ubuntu 上安装特定版本的 GCC,常见有几种方式,下面按推荐程度给你说明。
apt-cache search gcc | grep gcc-
或者:
apt list gcc-* 2>/dev/null
常见如:
例如安装 gcc-11:
sudo apt update
sudo apt install gcc-11 g++-11
gcc-11 --version
如果你装了多个版本,可以切换默认 gcc:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 110
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90
sudo update-alternatives --config gcc
g++ 同理:
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 110
sudo update-alternatives --config g++
sudo apt install software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install gcc-12 g++-12
适合:
sudo apt install build-essential wget
wget https://ftp.gnu.org/gnu/gcc/gcc-13.2.0/gcc-13.2.0.tar.gz
tar -xf gcc-13.2.0.tar.gz
cd gcc-13.2.0
./contrib/download_prerequisites
mkdir build && cd build
../configure --prefix=/opt/gcc-13 --enable-languages=c,c++ --disable-multilib
make -j$(nproc)
sudo make install
使用:
/opt/gcc-13/bin/gcc --version
| 需求 | 推荐方式 |
|---|---|
| 装 gcc-9 / 10 / 11 | apt |
| 多版本切换 | update-alternatives |
| apt 没有 | PPA |
| 任意版本 | 源码编译 |
如果你告诉我:
我可以直接给你精确命令。