Issue
Mac 10.13.6 High Sierra here. New to C development and I'm trying to get myself setup with the latest stable/recommended GCC version, which I believe (keep me honest here) is 10.2.
When I go to the terminal to see what I have installed:
$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.1.0 (clang-902.0.39.1)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$ gcc -dumpversion
4.2.1
OK...surprised to see LLVM/clang-related stuff in the output. So I try this:
$ clang --version
Apple LLVM version 9.1.0 (clang-902.0.39.1)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
So its almost as if... I have both clang
and gcc
installed, but my clang
installation has assimilated my GCC installation?! Why else would the gcc --version
output reference clang?
Is this typical for Mac setups?
What do I need to do to get GCC 10.2 properly installed on my machine?
Solution
Here are some simple truths, statements and observations to try and explain what's going on:
Apple ships the
clang/LLVM
compiler with macOS.Clang
is a "front-end" that can parseC
,C++
andObjective-C
down to something thatLLVM
(referred to as a "back-end") can compileClang/LLVM
is located in/Applications/Xcode.app/somewhere
Apple also ships a
/usr/bin/gcc
which just runsclang
. I have no idea why they do that - it doesn't seem very helpful to me - but they don't answer my questionsApple puts its binaries (programs) in
/usr/bin
. That is an integral part of macOS and you should never touch or change anything in there - you are asking for problems if you do. This warning applies to Python too.
If you want the real, lovely GNU Compiler Collection (GCC) which includes the gcc
, g++
and gfortran
compilers, your best bet, IMHO, is to get them from homebrew. I will not put the installation instructions here because they could become outdated, so you should use the ones on the homebrew site.
Once you have homebrew installed, you can install the GNU Compiler Collection (GCC) with:
brew install gcc
After that, you will have all the lovely GNU Compiler Collection (GCC) of tools in /usr/local/bin
, so you should put that in your PATH, near the beginning, and in any case before /usr/bin
, using:
export PATH=/usr/local/bin:$PATH
In general, you should also add a similar line into your login profile, or into the system login profile, so it is set up every time you or any other user logs in.
Let's take a look:
ls /usr/local/bin/gcc* /usr/local/bin/g++*
/usr/local/bin/gcc-10
/usr/local/bin/g++-10
Depending on the versions and updates, you will then have these programs available:
gcc-10 # the real GNU C compiler
g++-10 # the real GNU C++compiler
gfortran # GNU fortran compiler
And you can check their versions with:
gcc-10 -v
g++-10 -v
gfortran -v
Now you know about homebrew, here are some more simple truths and observations:
folks (who are not the supplier of the operating system) who supply binaries (programs) for you should put their stuff in
/usr/local
to show that it is just a locally installed program rather than a part of the core macOS operating systemhomebrew is well-behaved and installs its binaries (programs) in
/usr/local/Cellar
and then usually makes symbolic links from/usr/local/bin/PROGRAM
to the Cellar. None of this interferes with Apple-supplied stuff.if you want to run the homebrew version of a command, you should have
/usr/local/bin
first on your PATH
Let's have a look at those symbolic links:
ls -l /usr/local/bin/g*10
lrwxr-xr-x 1 mark admin 31 21 Aug 16:41 /usr/local/bin/g++-10 -> ../Cellar/gcc/10.2.0/bin/g++-10
lrwxr-xr-x 1 mark admin 31 21 Aug 16:41 /usr/local/bin/gcc-10 -> ../Cellar/gcc/10.2.0/bin/gcc-10
If you want to know what you are actually running when you enter a command, use the type
command like this.
type gcc
gcc is hashed (/usr/bin/gcc)
That tells you that if you run gcc
you will actually be running /usr/bin/gcc
which we know is from Apple - because it is in /usr/bin
Now try this:
type gcc-10
gcc-10 is hashed (/usr/local/bin/gcc-10)
That tells you that if you run gcc-10
you will actually be running /usr/local/bin/gcc-10
which we know is from homebrew - because it is in /usr/local/bin
Answered By - Mark Setchell Answer Checked By - David Marino (WPSolving Volunteer)