How to install GCC on macOS (and make it your default C/C++ compiler)

Clang is default installed C/C++ compiler on macOS but for some reason if you want to use GCC then these are installation steps (I did it on macOS Sierra 10.12.6):

1. Check if Clang is already installed on your system

Run command gcc --version, you should see something like this, gcc is just a symlink to clang:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

2. Install GCC with Homebrew

There are several versions of GCC available so you need to be sure which one you want to install. Try brew search gcc then brew info gcc, I just want the latest stable release of GCC now, it’s 8.2.0;

gcc: stable 8.2.0 (bottled), HEAD
GNU compiler collection
https://gcc.gnu.org/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/gcc.rb
==> Dependencies
Required: gmp , isl , libmpc , mpfr 
==> Options
--with-jit
 Build just-in-time compiler
--with-nls
 Build with native language support (localization)
--HEAD
 Install HEAD version
==> Analytics
install: 46,915 (30d), 207,277 (90d), 664,901 (365d)
install_on_request: 22,990 (30d), 92,797 (90d), 284,987 (365d)
build_error: 491 (30d)

Start installing GCC with brew install gcc.

After GCC is installed successfully, check again with gcc --version. Surprisingly you still see Clang!!! There is nothing wrong here, if you try again with gcc-8 --version then you will see what you expected:

gcc-8 (Homebrew GCC 8.2.0) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

3. Make GCC as default compiler

There are several ways to make it works but I went with this route since it’s the most safer and easier option. Add following content into your ~/.bash_profile, the export command just put /usr/local/bin ahead so it will be searched prior /usr/bin – where Clang symlinks created. alias commands allow you to run GCC conveniently without worrying about its version since its symlinks are created with -8 suffix (run ls -l /usr/local/bin to see all of them).

export PATH=/usr/local/bin:$PATH
alias gcc='gcc-8'
alias cc='gcc-8'
alias g++='g++-8'
alias c++='g++-8'
alias cpp='cpp-8'

UPDATE: I don’t know why aliases don’t work properly, if I run gcc directly from terminal then it works well – gcc-8 is used. But when I used a makefile, clang was still magically used, I only found this issue when I used a gcc only option – -fno-default-inline.

You can test it with this command g++ -fno-default-inline test.cpp -o test.out, there will be no warning. But if you run it from a Makefile, you will see a warning like this:

clang: warning: optimization flag '-fno-default-inline' is not supported [-Wignored-optimization-argument]

So instead of aliases, I just created symlinks for gcc in /usr/local/bin then everything worked well.

Updated ~/.bash_profile now:

export PATH=/usr/local/bin:$PATH

And create these symlinks:

cd /usr/local/bin
ln -s /usr/local/bin/gcc-8 cc
ln -s /usr/local/bin/gcc-8 gcc
ln -s /usr/local/bin/g++-8 g++
ln -s /usr/local/bin/c++-8 c++
ln -s /usr/local/bin/cpp-8 cpp

Restart your Terminal, run gcc --version again to see its correct version information and it’s done.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.