macosgccclang

Mac clang installation seems to override GCC install


I use macOS v10.13.6 (High Sierra) and am new to C development. I'm trying to get myself set up 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

Output:

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... I was surprised to see LLVM/clang-related stuff in the output. So I try this:

clang --version

Output:

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 it’s 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:


    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:

    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.