First erase the current update-alternatives setup for gcc and g++:

sudo update-alternatives --remove-all gcc 
sudo update-alternatives --remove-all g++

Install Packages

It seems that both gcc-4.3 and gcc-4.4 are installed after install build-essential. However, we can explicitly install the following packages:

sudo apt-get install gcc-4.3 gcc-4.4 g++-4.3 g++-4.4

Install Alternatives

Symbolic links cc and c++ are installed by default. We will install symbol links for gcc and g++, then link cc and c++ to gcc and g++ respectively. (Note that the 10, 20 and 30 options are the priorities for each alternative, where a bigger number is a higher priority.)

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 20

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.3 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.4 20

sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
sudo update-alternatives --set cc /usr/bin/gcc

sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
sudo update-alternatives --set c++ /usr/bin/g++

Configure Alternatives

The last step is configuring the default commands for gcc, g++. It's easy to switch between 4.3 and 4.4 interactively:

sudo update-alternatives --config gcc
sudo update-alternatives --config g++

Or switch using script:

#!/bin/sh

if [ -z "$1" ]; then
    echo "usage: $0 version" 1>&2
    exit 1
fi

if [ ! -f "/usr/bin/gcc-1" ]; then
    echo "no such version gcc/g++ installed" 1>&2
    exit 1
fi

update-alternatives --set gcc "/usr/bin/gcc-$1"
update-alternatives --set g++ "/usr/bin/g++-$1"
Answer from hhlp on askubuntu.com
🌐
MacPorts
ports.macports.org › port › gcc_select
Install gcc_select on macOS with MacPorts
It symlinks the standard compiler executables in the MacPorts prefix to the selected version. gcc_select installs files that allow 'port select' to switch the default version of gcc.
🌐
MacPorts
ports.macports.org › port › gcc_select › details
gcc_select | MacPorts
It symlinks the standard compiler executables in the MacPorts prefix to the selected version. gcc_select installs files that allow 'port select' to switch the default version of gcc.
Top answer
1 of 10
411

First erase the current update-alternatives setup for gcc and g++:

sudo update-alternatives --remove-all gcc 
sudo update-alternatives --remove-all g++

Install Packages

It seems that both gcc-4.3 and gcc-4.4 are installed after install build-essential. However, we can explicitly install the following packages:

sudo apt-get install gcc-4.3 gcc-4.4 g++-4.3 g++-4.4

Install Alternatives

Symbolic links cc and c++ are installed by default. We will install symbol links for gcc and g++, then link cc and c++ to gcc and g++ respectively. (Note that the 10, 20 and 30 options are the priorities for each alternative, where a bigger number is a higher priority.)

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 20

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.3 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.4 20

sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
sudo update-alternatives --set cc /usr/bin/gcc

sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
sudo update-alternatives --set c++ /usr/bin/g++

Configure Alternatives

The last step is configuring the default commands for gcc, g++. It's easy to switch between 4.3 and 4.4 interactively:

sudo update-alternatives --config gcc
sudo update-alternatives --config g++

Or switch using script:

#!/bin/sh

if [ -z "$1" ]; then
    echo "usage: $0 version" 1>&2
    exit 1
fi

if [ ! -f "/usr/bin/gcc-1" ]; then
    echo "no such version gcc/g++ installed" 1>&2
    exit 1
fi

update-alternatives --set gcc "/usr/bin/gcc-$1"
update-alternatives --set g++ "/usr/bin/g++-$1"
2 of 10
62

execute in terminal :

gcc -v
g++ -v

Okay, so that part is fairly simple. The tricky part is that when you issue the command GCC it is actually a sybolic link to which ever version of GCC you are using. What this means is we can create a symbolic link from GCC to whichever version of GCC we want.

  • You can see the symbolic link :
ls -la /usr/bin | grep gcc-4.4
ls -la /usr/bin | grep g++-4.4
  • So what we need to do is remove the GCC symlink and the G++ symlink and then recreate them linked to GCC 4.3 and G++ 4.3:
rm /usr/bin/gcc
rm /usr/bin/g++

ln -s /usr/bin/gcc-4.3 /usr/bin/gcc
ln -s /usr/bin/g++-4.3 /usr/bin/g++
  • Now if we check the symbolic links again we will see GCC & G++ are now linked to GCC 4.3 and G++ 4.3:
ls -la /usr/bin/ | grep gcc
ls -la /usr/bin/ | grep g++
  • Finally we can check our GCC -v again and make sure we are using the correct version:
gcc -v
g++ -v
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › Option-Summary.html
Option Summary (Using the GNU Compiler Collection (GCC))
-fcall-saved-reg -fcall-used-reg -ffixed-reg -fexceptions -fnon-call-exceptions -fdelete-dead-exceptions -funwind-tables -fasynchronous-unwind-tables -fno-gnu-unique -finhibit-size-directive -fcommon -fno-ident -fpcc-struct-return -fpic -fPIC -fpie -fPIE -fno-plt -fno-jump-tables -fno-bit-tests -frecord-gcc-switches -freg-struct-return -fshort-enums -fshort-wchar -fverbose-asm -fpack-struct[=n] -fleading-underscore -ftls-model=model -fstack-reuse=reuse_level -ftrampolines -ftrampoline-impl=[stack|heap] -ftrapv -fwrapv -fwrapv-pointer -fvisibility=[default|internal|hidden|protected] -fstrict-volatile-bitfields -fsync-libcalls -fzero-init-padding-bits=value -Qy -Qn
🌐
LinuxConfig
linuxconfig.org › home › how to switch between multiple gcc and g++ compiler versions on ubuntu 20.04 lts focal fossa
How to switch between multiple GCC and G++ compiler versions on Ubuntu 20.04 LTS Focal Fossa
September 21, 2025 - sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 7 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 7 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 8 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 8 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 9 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 9 · Check the available C and C++ compilers list on your Ubuntu 20.04 system and select desired version by entering relevant selection number:
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › Overall-Options.html
Overall Options (Using the GNU Compiler Collection (GCC))
Print (on the standard output) a description of the command-line options understood by gcc. If the -v option is also specified then --help is also passed on to the various processes invoked by gcc, so that they can display the command-line options they accept.
🌐
GNU
gcc.gnu.org › onlinedocs › gccint › Selectors.html
Selectors (GNU Compiler Collection (GCC) Internals)
Each of opt1 to optn is a space-separated list of option globs. The selector expression evaluates to true if, for one of these strings, every glob in the string matches an option that was passed to the compiler.
Find elsewhere
Top answer
1 of 9
147

As @Tommy suggested, you should use update-alternatives.
It assigns values to every software of a family, so that it defines the order in which the applications will be called.

It is used to maintain different versions of the same software on a system. In your case, you will be able to use several declinations of gcc, and one will be favoured.

To figure out the current priorities of gcc, type in the command pointed out by @tripleee's comment:

update-alternatives --query gcc

Now, note the priority attributed to gcc-4.4 because you'll need to give a higher one to gcc-3.3.
To set your alternatives, you should have something like this (assuming your gcc installation is located at /usr/bin/gcc-3.3, and gcc-4.4's priority is less than 50):

update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-3.3 50

--edit--

Finally, you can also use the interactive interface of update-alternatives to easily switch between versions. Type update-alternatives --config gcc to be asked to choose the gcc version you want to use among those installed.

--edit 2 --

Now, to fix the CXX environment variable systemwide, you need to put the line indicated by @DipSwitch's in your .bashrc file (this will apply the change only for your user, which is safer in my opinion):

echo 'export CXX=/usr/bin/gcc-3.3' >> ~/.bashrc
2 of 9
82

Here's a complete example of jHackTheRipper's answer for the TL;DR crowd. :-) In this case, I wanted to run g++-4.5 on an Ubuntu system that defaults to 4.6. As root:

apt-get install g++-4.5
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6 100
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.5 50
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 100
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.5 50
update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.6 100
update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.5 50
update-alternatives --set g++ /usr/bin/g++-4.5
update-alternatives --set gcc /usr/bin/gcc-4.5
update-alternatives --set cpp-bin /usr/bin/cpp-4.5

Here, 4.6 is still the default (aka "auto mode"), but I explicitly switch to 4.5 temporarily (manual mode). To go back to 4.6:

update-alternatives --auto g++
update-alternatives --auto gcc
update-alternatives --auto cpp-bin

(Note the use of cpp-bin instead of just cpp. Ubuntu already has a cpp alternative with a master link of /lib/cpp. Renaming that link would remove the /lib/cpp link, which could break scripts.)

🌐
GitHub
github.com › actions › virtual-environments › issues › 2503
How to select install gcc version? · Issue #2503 · actions/runner-images
January 20, 2021 - matrix: runs-on: [ubuntu-latest, macos-latest, windows-latest] include: - runs-on: ubuntu-latest compiler: gcc gcc: 8
Author   actions
🌐
Gentoo Forums
forums.gentoo.org › viewtopic-t-859408-start-0.html
Gentoo Forums :: View topic - Multiple switchable gcc version
January 9, 2011 - FAQ | Search | Memberlist | Usergroups | Statistics | Profile | Log in to check your private messages | Log in | Register · Links: forums.gentoo.org | www.gentoo.org | bugs.gentoo.org | wiki.gentoo.org | forum-mods@gentoo.org
🌐
NixOS Discourse
discourse.nixos.org › t › how-to-select-a-specific-version-of-gcc-within-a-flake-nix › 23372
How to select a specific version of gcc within a flake.nix - NixOS Discourse
November 21, 2022 - I’m attempting to set a specific version of gcc (gcc 10.x) from with in a flake, but not having much luck. I’ve tried a number of different things, and I can see the package get loaded ‘pkgs.gcc8’ – loads gcc8 package, …
🌐
Mengyang's blog
archerfmy.github.io › 2017 › 04 › 12 › How-to-switch-your-gcc-g-version-in-ubuntu
How to switch your gcc/g++ version in ubuntu | Mengyang's blog
May 22, 2017 - If you can see /usr/bin/gcc-4.5, see step 4. ... Press ENTER to maintain, or type the selection number to the corresponding version.
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › Invoking-GCC.html
Invoking GCC (Using the GNU Compiler Collection (GCC))
The usual way to run GCC is to run the executable called gcc, or machine-gcc when cross-compiling, or machine-gcc-version to run a specific version of GCC. When you compile C++ programs, you should invoke GCC as g++ instead.
Top answer
1 of 2
14

On macOS Catalina (and prior versions, and most likely subsequent versions too), there are two aspects to the problem and some suggested solutions.

What is the name of the compiler used by make by default?

$ mkdir junk
$ cd junk
$ > x.cpp
$ > y.c
$ make x y
c++     x.cpp   -o x
cc     y.c   -o y
$ cd ..
$ rm -fr junk

This shows that the names used by make are cc and c++. Those are not obviously clang or clang++, but neither are they obviously gcc and g++.

$ which cc c++
/usr/bin/cc
/usr/bin/c++

Which compiler is it really?

Which compiler really lives behind the names cc, c++, gcc, g++, clang, and clang++? We can check which compiler these really are by getting them to identify their version:

$ for compiler in cc c++ gcc g++ clang clang++
> do
>     which $compiler
>     $compiler --version
> done
/usr/bin/cc
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/c++
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/gcc
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/g++
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/clang
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/clang++
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

As you can see, the versions installed in /usr/bin are all the same compiler, and that compiler is clang or clang++.

This was run on a machine with macOS Mojave 10.14.6 and XCode 11.3.1. The latest version of XCode — 11.4.1 — is only available on Catalina. However, the general conclusion is the same — all the C and C++ compilers are really clang and clang++ in disguise.

How do you get GNU GCC onto your machine?

How do you get a real GNU GCC — a real GCC, not clang in disguise — onto your machine?

  • Use Brew to install GCC (I've not checked which version of GCC is current).
  • Or use MacPorts (again, I've not checked which version of GCC is current).
  • If you're adventuresome, do it yourself (but I've not yet succeeded at building GCC 9.3.0 on Catalina; I have a GCC 9.2.0 built on macOS Mojave 10.14.x that works OK on Catalina though — with one environment variable needed to locate the headers).
  • Maybe Fink — it lists GCC 8.4 as being made available in 2020; I don't know about newer versions.

Be aware that Apple has taken to hiding the system header files miles out of the way (not in /usr/include — and you can't modify that part of the file system to add a symlink to where they've hidden them):

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include

(You mean you couldn't guess that? Me neither!)

How do you change the default compiler?

Once you have GCC installed somewhere appropriate, you need to ensure you use the 'real' GCC and not the 'fake' in /usr/bin. You do that in part by ensuring that the bin directory for the 'real' GCC occurs on your PATH before /usr/bin. I have GCC 9.3.0 installed under /opt/gcc/v9.3.0, so /opt/gcc/v9.3.0/bin appears on my PATH long before /usr/bin does.

You also need to ensure that the configuration for riak (the software you're installing) uses the correct compilers. If there's a ./configure script, run it with the correct path specified for the compilers. For example, I might use:

./configure CC=/opt/gcc/v9.3.0/bin/gcc CXX=/opt/gcc/v9.3.0/bin/g++

You can also set these values as environment variables.

If it uses cmake or some other configuration package, you'll need to consult the installation instructions. That's usually README or sometimes INSTALL.


See also (increasingly older posts):

  • Can't compile a C program on a Mac after upgrade to Catalina
  • Can't compile a C program on a Mac after upgrade to Mojave
  • Install GNU GCC on a Mac
2 of 2
4

For macOS, follow the below steps:

Method 1

  1. Install the GCC compiler using Homebrew (install Homebrew if you haven't already)

     brew install gcc
    
  2. Suppose Homebrew installs the GCC 12 version on your system. Check using:

      gcc-12 --version
    
  3. Open a terminal and use the following command:

       cd /opt/homebrew/bin
    
  4. Below, the given command will make a symbolic link for g++ with g++-12. Using this, you can easily switch between different versions

       ln -s gcc-12 gcc
    

Now check gcc --version, it will be changed from macOS default Clang to GCC.

Method 2 (creating aliases)

  1. Open Terminal and open either the .zprofile or .zshrc file file

    open .zprofile
    
  2. After opening the file, add the below lines, save and close it.

    alias gcc="gcc-13"
    alias g++="g++-13"
    
  3. Restart the terminal, and it will work fine with using just gcc or g++

Note: you can do the same steps for changing g++ also, just replace gcc-12 with g++-12

Also maybe at the time you'll see this answer, Homebrew installs the latest gcc or g++ version like gcc-13, etc., so make sure to modify the commands accordingly.

  • 5 May 2023: Now, by default Homebrew installs the latest gcc-13 version, so use the above commands accordingly.
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › x86-Options.html
x86 Options (Using the GNU Compiler Collection (GCC))
While picking a specific cpu-type schedules things appropriately for that particular chip, the compiler does not generate any code that cannot run on the default machine type unless you use a -march=cpu-type option. For example, if GCC is configured for i686-pc-linux-gnu then -mtune=pentium4 generates code that is tuned for Pentium 4 but still runs on i686 machines.
🌐
Visual Studio Code
code.visualstudio.com › docs › cpp › config-mingw
Using GCC with MinGW
November 3, 2021 - The extension looks in several common compiler locations but will only automatically select one that is in either one of the "Program Files" folders or whose path is listed in the PATH environment variable. If the Microsoft Visual C++ compiler can be found it will be selected, otherwise it will select a version of gcc, g++, or clang.
🌐
GitHub
gist.github.com › cobaohieu › ded429cb892b46ae9bfd9919a11e593a
update-alternatives for gcc on Ubuntu - Gist - GitHub
$ ls -larth `which gcc`* lrwxrwxrwx 1 root root 23 Thg 1 4 2019 /usr/bin/gcc-7 -> aarch64-linux-gnu-gcc-7 lrwxrwxrwx 1 root root 23 Thg 3 10 2020 /usr/bin/gcc-8 -> aarch64-linux-gnu-gcc-8 lrwxrwxrwx 1 root root 23 Thg 4 23 2020 /usr/bin/gcc-9 -> aarch64-linux-gnu-gcc-9 lrwxrwxrwx 1 root root 24 Thg 4 15 19:21 /usr/bin/gcc-10 -> aarch64-linux-gnu-gcc-10 lrwxrwxrwx 1 root root 24 Thg 4 29 01:05 /usr/bin/gcc-11 -> aarch64-linux-gnu-gcc-11 lrwxrwxrwx 1 root root 21 Thg 5 27 09:22 /usr/bin/gcc -> /etc/alternatives/gcc