在 Debian 中切换 gcc 版本,通常有以下几种常用方法,最推荐的是 update-alternatives 方式。
update-alternatives(推荐)sudo apt update
sudo apt install gcc-9 gcc-10 gcc-11
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 110
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-11 110 auto mode
1 /usr/bin/gcc-9 90 manual mode
2 /usr/bin/gcc-10 100 manual mode
3 /usr/bin/gcc-11 110 manual mode
Press <enter> to keep the current choice[*], or type selection number:
选择对应数字即可。
gcc --version
如果你用 C++,也要切换 g++:
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 90
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 100
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 110
sudo update-alternatives --config g++
gcc-9 test.c -o test
或
CC=gcc-10 make
ls /usr/bin/gcc*
dpkg -l | grep gcc
make clean)如果你告诉我 Debian 版本(如 11 / 12) 和 想用的 gcc 版本,我可以给你更精确的命令。