How to change MacOS Default from clang to homebrew gcc - c++

I'm having trouble finding how to change the default c++ compiler from clang to the homebrew version of gcc I have installed. The end goal here is to be able to run a command line gradle build using gcc.
I've checked to make sure that the first directory in the PATH variable is /usr/local/bin as suggested in some places, but it still defaults to clang/LLVM

Related

Update GCC on Ubuntu

I am on a project that needs GCC 10.x or later.
At this time I have GCC 9.4.0 on Ubuntu 20.04.1. I tried to update the compiler, but it does not work.
Can anybody give me an advice for the update?
I read on the gcc website that version 9.4 is more up-to-date than some 10.x versions. How is Gcc structured?
among other things I tried:
sudo apt-get install gcc-10.2 g++-10.2
but after all my gcc version is still 9.4
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
This is a common pattern in linux. When there are multiple versions of the same program installed, though the executables are all present in the /usr/bin/ directory, only one of them is "visible" as that program. For example, if you install gcc-9 and gcc-10, both executables are present as /usr/bin/gcc-9 and /usr/bin/gcc-10 but only one of them is visible as gcc. This happens by symlinking a preferred version to the same directory as /usr/bin/gcc. In ubuntu 20.04, the preferred version is gcc-9 and so, gcc-9 is symlinked as gcc.
You can check this by running the following command.
$ which gcc | xargs file
The output will be
/usr/bin/gcc: symbolic link to gcc-9
There are a few things you can do to use gcc-10 as your c compiler.
Directly call the gcc-10 executable. Instead of using gcc <code.c>, call gcc-10 <code.c>.
You can manually symlink gcc-10 as the preferred gcc. Assuming you did not modify the system paths, the following command can be used.
# ln -s /usr/bin/gcc-10 /usr/local/bin/gcc
This works because, by default, the executables in /usr/local/bin/ take precedence over /usr/bin/.
If you are using bash, you can create an alias for gcc as gcc-10. Add the following line to your .bashrc.
alias gcc="gcc-10"
Remember to relaunch bash or source ~/.bashrc.
Using update-alternatives (Thanks to #ted-lyngmo for pointing it out). Debian based distributions supply a separate program, that can make symlinking easier / more functional. Read more using man update-alternatives. To use gcc-10 as the preferred gcc, use the following command.
# update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 60
The above command says, /usr/bin/gcc is the link needed and the name is gcc, the target executable is /usr/bin/gcc-10 and it has a priority of 60.
This links gcc to /etc/alternatives/gcc, which itself is a symlink to /usr/bin/gcc-10. If a higher priority program is added to update-alternatives, /etc/alternatives/gcc points to the higher priority program.
If you don't have any specific reason, I would also recommend to upgrade to a newer ubuntu version, so that the default gcc is a newer one.
I read on the gcc website that version 9.4 is more up-to-date than some 10.x versions.
With newer gcc versions, new features are added. Support for newer c/c++ standards are also added. Eg. You can read the changes for gcc-10 here. But people still need gcc-9 because some programs only build with gcc-9. So, GNU maintains gcc-9 (and much older versions) for a long time. Bugs are fixed, and newer releases are made. This can happen after the release of a newer gcc version. So, it is very much possible that a version of gcc-9 is newer than a version of gcc-10.

building gcc 7 (head) from repository on macos sierra?

I have homebrew gcc 6 installed, but would like to play with gcc 7 on my mac running sierra. However, instead of the simple configure make that worked in the past, things have changed so much with sierra that I no longer can figure out, how to get gcc compiling itself. I understand that what I checked out from the repo is not supposed to work, but at least I should be able to compile it. The current roadblock is that there is no longer a /usr/include with the system headers and I didn't find the option to point configure/make to use the XCode.app internal one that apperently my homebrew gcc6 knows about.
What is the option to provide to configure to set the directory for system includes (and may be libraries) to get a current gcc compile on macos Sierra?
You need to install the "Xcode command line tools" from the app store.
That installs an assembler and linker, and the headers in /usr/include/, and then the GCC build works as expected.

Can I have gcc/g++ on MacOS X 10.11 pointing to the ACTUAL gcc/g++?

This is really annoying. For some reason, on MacOS X 10.11 (probably also on previous versions) there are gcc and g++ commands (in /usr/bin, they are not aliases or so) which, when executed with the -v argument, give:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.2.0
Thread model: posix
So it looks like they actually execute the clang and clang++ compilers by apple. Now, I really need my computer to execute gcc and g++ when I invoke those commands, both from the terminal and through makefiles.
The reasons for this are two:
I like to have my computer do what I ask it to do.
Apparently clang++ compiles stuff using a different c++ standard library from g++, and this is causing me problems when I compile and link my stuff with g++-5 (the ACTUAL g++, installed via homebrew) against CppUnit.
Does anybody know what is the best way to have gcc and g++ actually call gcc and g++?
Include in your PATH, before /usr/bin, a directory that contains a symbolic link named gcc pointing to /…/bin/gcc-5.
The latest Mac OS X does not let you change /usr, from what I hear, so this conservative solution is the only one available.
when I invoke those commands, both from the terminal and through makefiles.
If you adjust your PATH variable in your .profile, both these cases will be covered.
Apple does not actually provide gcc or g++, although (perhaps only misguided) they make aliases to pretend that clang is the same.
You can install gcc and g++ with MacPorts (also with homebrew). I use MacPorts, which puts its executables in /opt/local/bin.
With MacPorts, I see these currently-available ports (programs that have to be compiled to work on one's machine), using this command
port list |grep gcc
gcc410 #5-20140817 lang/gcc410
gcc43 #4.3.6 lang/gcc43
gcc44 #4.4.7 lang/gcc44
gcc45 #4.5.4 lang/gcc45
gcc46 #4.6.4 lang/gcc46
gcc47 #4.7.4 lang/gcc47
gcc48 #4.8.5 lang/gcc48
gcc49 #4.9.3 lang/gcc49
gcc5 #5.2.0 lang/gcc5
gcc6 #6-20151129 lang/gcc6
gccxml-devel #20150423 lang/gccxml-devel
gcc_select #0.1 sysutils/gcc_select
gccmakedep #1.0.3 x11/gccmakedep
According to its webpage, brew would do something similar, but install into /usr/local/bin.
When I installed MacPorts, its installer updated my ~/.profile, adding this to update PATH:
# MacPorts Installer addition on 2015-10-03_at_14:17:30: adding an appropriate PATH variable for use with MacPorts.
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
# Finished adapting your PATH environment variable for use with MacPorts.
Each of those ports from MacPorts installs gcc with a different name, and the port script has a feature select which establishes a symbolic link, e.g., from gcc to gcc49. brew has something similar. According to How can I brew link a specific version?, you would use
brew switch gcc-package-name package-version
e.g., (guessing at a valid name)
brew switch gcc 4.9

How to setup CLion with portable Clang on ubuntu?

I want to test Clang with CLion in ubuntu. By default Clion detects my gcc installation, but I want to use clang instead of gcc. Moreover, I don't want to install clang via apt-get. I have downloaded llvm, clang (3.6.2) binary from llvm website. I want to use that portable clang binaries.
Here is my system setup:
Ubuntu - 14.04
gcc - 4.8.4
llvm clang - 3.6.2 (portable)
CLion - 1.0.4
So how to set up CLion with llvm-clang here?
I got the answer from CLion blog and it works and here it goes.
To provide CMake compiler paths, go to Settings | Build, Execution, Deployment | CMake and pass as CMake options:
-D CMAKE_C_COMPILER=
-D CMAKE_CXX_COMPILER=
In case CMake fails to find some path to clang libs, etc. you can also set there environment variables:
CC=/usr/bin/clang
CXX=/usr/bin/clang++
The FAQ states the following:
At present CLion supports GCC and Clang compilers and is guided by these two to get the libraries and headers paths. In the next releases we are planning to extend the list of compilers available in CLion.
To change the compiler, go to the Cache tab in CMake tool window and set the compiler’s path to the CMAKE_CXX_COMPILER variable. Then press Enter and click the Apply Changes and Reload button

OpenMP on Mac not working anymore

I had OpenMP compiling and executing in C/C++ on my Mac but then I formatted and reinstalled a fresh copy of OS X. I believe the only difference in the setup was that I had Xcode 5 before the format, and now I have Xcode 6.
Now I get fatal error: 'omp.h' file not found
I read this has something to do with clang and gcc, but, my confusion lies with why did it work before and now its not working?
After some research I seem to have found an answer.
GCC included in the latest version of Xcode (Xcode 6) is only a symbolic link to clang. Since clang does not support OpenMP at this time, you need to install a different version of GCC. The easiest way to do this would be to use Homebrew or MacPorts.
Keep in mind that even when you do this you will probably need to alter your $PATH to have /usr/local come before /usr/bin. This is because Homebrew will have placed your newly installed GCC in /usr/local. Also, some implementations may name the command gcc-49 instead of plain gcc.
Mac OSX uses clang.The gcc compiler in OS X does not support OpenMP. To use this feature a new gcc compiler needs to be installed.
Go to Terminal, if you have not installed Hombrew, install it:
/usr/bin/ruby -e "$(curl -fsSL https://`enter code here`raw.githubusercontent.com/Homebrew/install/master/install)"
then install new version of gcc
brew reinstall gcc --without-multilib
This will not make changes to the existing gcc compiler installed by Xcode as we are reinstalling it.
After running the command given below compile the files using the new version of gcc using the syntax : gcc-version -fopenmp filename.c
To find the version type gcc and then press tab. This will list out all the possible variants of gcc. The version number can be found out from this. For example : gcc-6, gcc-4.9,etc