How do I make sure that my default C/C++ compiler is GCC - c++

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

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

Related

How to resolve `clang: error: unsupported option '-fsanitize=leak'` on apple devices

I am trying to run a simple leak check program.
#include <iostream>
int main()
{
double *ptr = new double(3.14);
}
using the command
g++ -g -fsanitize=leak -o main main.cpp
and I get the following error:
clang: error: unsupported option '-fsanitize=leak' for target 'x86_64-apple-darwin20.1.0'
I stopped using the clang that comes with Xcode and installed clang/LLVM using homebrew.
$ which clang++
/usr/local/opt/llvm/bin/clang++
clang++ --version
clang version 11.0.0
Target: x86_64-apple-darwin20.1.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin
EDIT: When I was using apple clang, g++ used to default to clang++. Apparently that changed when I installed llvm/clang. Thanks to #cigien for pointing it out. g++ still uses default to the compiler that that comes with Apple clang.
g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.27)
Target: x86_64-apple-darwin20.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
According to this answer you should use:
g++-10 -g -fsanitize=leak -o main main.cpp (in this cases (with flag leak) there is no leak message for me ./main, but it compile) then it should be:
g++-10 -fsanitize=address -g main.cpp ; ASAN_OPTIONS=detect_leaks=1 ./a.out and it detect leak well.
Note, that the correct path of brew installed g++ in MacOS is:
$ which g++-10
> /usr/local/bin/g++-10
--
$ which g++
> /usr/bin/g++ //this is pseudonym of clang
The same for gcc-10 (10 is my current version. You should use your version instead of that)
If you use CMakeLists.txt file you will configure it like this:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=[sanitizer_name] [additional_options] [-g] [-OX]")
# leak sanitizer_name not works for me. should be address
And should execute cmake command like this:
cmake -DCMAKE_C_COMPILER=/usr/local/bin/gcc-10 -DCMAKE_CXX_COMPILER=/usr/local/bin/g++-10 ..
And then ASAN_OPTIONS=detect_leaks=1 ./a.out
Note, that if you open */CMakeFiles/3.18.4/CMakeCXXCompiler.cmake file you will observe the compiled info, and now it will be g++.

What version of LLVM does the latest emscripten use?

What version of LLVM does the latest emscripten use?
I googled for this quite a while but could not find any info. The background is that I've got some C++ code that requires clang-5.0 - what are the chances that it would compile with emscripten? Or at least code that needs clang-4.0?
The latest version of emscripten uses clang version 4:
$ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 1.37.21
clang version 4.0.0 (https://github.com/kripken/emscripten-fastcomp-clang.git 974b55fd84ca447c4297fc3b00cefb6394571d18) (https://github.com/kripken/emscripten-fastcomp.git 087c6b7b18b7b769d4ad8f2ac3e0dd0ae6b924c2) (emscripten 1.37.21 : 1.37.21)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
However, you can build WebAssembly without emscripten, by using the clang, llc, s2wasm and wasm tools separately as documented in this gist.
Another good alternative is the wasm-toolchain project:
https://github.com/tpimh/wasm-toolchain
With this installed, I have clang version 6:
$ ./clang --version
clang version 6.0.0 (http://llvm.org/git/clang.git 9411957410813aeefc1bb299abcb856ce1ef8aae) (http://llvm.org/git/llvm.git 68b21d6108df63d65d6735e9686d53cca844e37a)
Target: x86_64-apple-darwin16.7.0
Thread model: posix

os x LLVM supports c++11/14, why the include directory is still gcc 4.2.1?

On my new macbook pro, after install xcode, I found:
Both gcc and clang are linking to LLVM 7.3. I guess gcc is just an alias of clang here, right?
As long as gcc supports -std=c++11 and -std=c++14 parameter, why the /usr/include/c++ only has a directory named "4.1.2"? I know gcc 4.1.2 is quite an old version,

which gcc am I using?

I am attempting to compile a package on my Mac laptop and my Mac Mini desktop. It compiles successfully on the laptop, but not the mini (gives the following error: gcc: error: unrecognized command line option '-Xarch_x86_64'). Both machines are running OS X Yosemite (10.10.2). On both machines, when I type
which gcc
I get:
gcc: aliased to nocorrect gcc
(I don't know what this means) When I type
echo $PATH
I get:
/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
This makes me think that if a gcc is found in /usr/local/bin, that's the one that will be my gcc. When I type
ls -l /usr/local/bin/gcc
I get:
/usr/local/bin/gcc -> /usr/local/bin/gcc-4.8
on both machines. However, when I type
gcc --version
on the Mini I get:
gcc (Homebrew gcc48 4.8.4) 4.8.4
Copyright (C) 2013 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.
and on the laptop I get:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
Which is the version installed by XCode. (This is what I get on both machines from /usr/bin/gcc --version)
How is the laptop using gcc 4.2 when the first gcc in my path is a symlink to gcc 4.8, and how do I make the mini do the same?
The nocorrect prefix is a zsh construct that will inhibit spelling correction.
It looks like you may have installed a specific gcc version with homebrew on the Mac Mini and you're using the system compiler (or rather, the one installed with XCode command line tools) on the laptop.
If you don't want to use the gcc installed with homebrew, you can just do:
brew unlink gcc
That will make the symlink go away and you'll use the next compiler. Which is hopefully the one you want.
When which gcc returns something like gcc: aliased to nocorrect gcc, the way to get the actual path would be to unalias foo, like this:
$ unalias gcc
$ which gcc
/usr/bin/gcc
This will remove the alias for the current shell, and you'll have to open up a new terminal or re-alias it yourself.
If you'd rather not do that, then you can use bash to determine the where the command is coming from:
$ bash -c 'which gcc'
/usr/bin/gcc

How to emit optimisation reports using Apple clang 3.5?

According to the documentation, clang supports options to emit optimisation reports:
When the pass makes a transformation (-Rpass).
When the pass fails to make a transformation (-Rpass-missed).
When the pass determines whether or not to make a transformation (-Rpass-analysis).
They provide the following example command line:
$ clang -O2 -Rpass=inline code.cc -o code
When I try this, I get an error:
$ clang src/test/tests.cpp -Rpass=inline
clang: error: unknown argument: '-Rpass=inline'
scons: *** [build/test/tests.o] Error 1
scons: building terminated because of errors.
My clang version is:
$ clang --version
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix
Is this option not available on Mac OS? Is the documentation erroneous? Or am I doing it wrong? If so, how to do it right?
Works for Debian clang-3.5.0-6, so this is probably Apple clang restriction.
% clang++ -O2 -Rpass=inline foo.cpp
foo.cpp:11:2: remark: _ZN1CC2Ev inlined into main [-Rpass=inline]
P* p = new C();
^
% clang --version
Debian clang version 3.5.0-6 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
Target: i386-pc-linux-gnu
Thread model: posix