5

I'm trying to install Riak from source on macOS (https://docs.riak.com/riak/kv/2.2.3/setup/installing/mac-osx.1.html#installing-from-source).

There is a note:

Riak will not compile with Clang. Please make sure that your default C/C++ compiler is GCC

How do I find out which compiler is the default and how to change it?

macOS Catalina (10.15.4), which command prints:

$ which clang
/usr/bin/clang
$ which gcc
/usr/bin/gcc
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Nikita Protskiy
  • 75
  • 1
  • 1
  • 5
  • I'd assume the make file is clever enough to pick the right compiler if it's installed. In any case why not just try and see if it works? – Voo Apr 18 '20 at 16:29
  • Please check this link : https://stackoverflow.com/questions/7031126/switching-between-gcc-and-clang-llvm-using-cmake – executable Apr 18 '20 at 16:41
  • Does this answer your question? [Switching between GCC and Clang/LLVM using CMake](https://stackoverflow.com/questions/7031126/switching-between-gcc-and-clang-llvm-using-cmake) – Adrian Mole Apr 18 '20 at 20:09

2 Answers2

9

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):

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
2

For macOs follow below steps:

Method-1

  1. Install gcc compiler using homebrew. (install homebrew if you haven't already)

     brew install gcc
    
  2. Suppose brew installs gcc-12 version on your system, check using:

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

       cd /opt/homebrew/bin
    
  4. Below 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 .zprofile or .zshrc file

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

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

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

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

  • 5 May 2023: Now, by default brew installs the latest gcc-13 version, so use above commands accordingly.
princebansal_
  • 133
  • 3
  • 9