Update GCC on OSX
If you install macports you can install gcc select, and then choose your gcc version.
/opt/local/bin/port install gcc_select
To see your versions use
port select --list gcc
To select a version use
sudo port select --set gcc gcc40
Installing GCC on macOS Catalina
Probably not the elegant answer, but it worked. I was able to find the header files needed using $(xcrun --show-sdk-path) I then copied them all to /usr/local where the gcc location was expecting them to be. Now all works.
OSX - replace gcc version 4.2.1 with 4.9 installed via Homebrew
By default, homebrew
places the executables (binaries) for the packages it installs into /usr/local/bin
- which is a pretty sensible place for binaries installed by local users when you think about it - compared to /bin
which houses standardisded binaries belonging to the core OS. So, your brew
command should have installed gcc-4.9
into /usr/local/bin
. The question is now how to use it... you have several options.
Option 1
If you just want to compile one or two things today and tomorrow, and then probably not use the compiler again, you may as well just invoke the gcc
installed by homebrew
with the full path like this:
/usr/local/bin/gcc-4.9 --version
Option 2
If you are going to be using gcc
quite a lot, it gets a bit tiresome explicitly typing the full path every time, so you could put the following into your ~/.bash_profile
export PATH=/usr/local/bin:$PATH
and then start a new Terminal and it will know it needs to look in /usr/local/bin
, so you will be able to get away with simply typing
gcc-4.9 --version
Option 3
If you just want to use gcc
to invoke the compiler, without worrying about the actual version, you can do Option 2 above and additionally create a symbolic link like this
cd /usr/local/bin
ln -s gcc-4.9 gcc
That will allow you to run the homebrew
-installed gcc
by simply typing gcc
at the command line, like this
gcc --version
Note:
If you later want to install, say gcc-4.13
or somesuch, you would do your brew install
as before, then change the symbolic link like this:
cd /usr/local/bin
rm gcc # remove old link from gcc to gcc-4.9
ln -s gcc-4.13 gcc # make new link from gcc to gcc-4.13
Note that if you are actually using C++
rather than C
, you will need to adapt the above for g++
in place of gcc
.
Mac clang installation seems to override GCC install
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
Can't compile c++ program using gcc on Mac after updating to Monterey
I also had the same problem and in my case the problem was outdated
command line developer tools.
I found out it by running
$ brew doctor
...
Warning: Your Command Line Tools are too outdated.
Update them from Software Update in System Preferences or run:
softwareupdate --all --install --force
If that doesn't show you any updates, run:
sudo rm -rf /Library/Developer/CommandLineTools
sudo xcode-select --install
Alternatively, manually download them from:
https://developer.apple.com/download/all/.
You should download the Command Line Tools for Xcode 13.1.
I tried to update them via softwareupdate -l
, however this did not give me any update information.
So removed them and reinstalled with xcode-select --install
.
After this g++-11
stopped giving me an error about this header.
Now brew config
tells me that I have CLT: 13.0.0.0.1.1627064638
P.S.
Also I started the xcode and it asked me whether additional components need to be installed and I said yes.
How do I upgrade gcc's Clang version in OS X?
Install Xcode, which will give you the latest available versions of both:
xcode-select --install
...
$ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$ clang --version
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Related Topics
How to Make Objective-C Class Using Std:Vector Made Available to Swift Classes
How to Find All the Functions Exposed by a Dll
Best Method for Storing This Pointer for Use in Wndproc
Why Is the Copy-Constructor Argument Const
Checking If an Iterator Is Valid
Why Do We Need to Use 'Int Main' and Not 'Void Main' in C++
Multiple Definition of Inline Functions When Linking Static Libs
Must the Int Main() Function Return a Value in All Compilers
Converting Epoch Time to "Real" Date/Time
How to Set a Breakpoint in Gdb That Is Conditional on the Call Stack
Advantages of Std::For_Each Over for Loop
Return Value of Operator Overloading in C++
Is There Actually a Reason Why Overloaded && and || Don't Short Circuit
Compiling with -Static-Libgcc -Static-Libstdc++ Still Results in Dynamic Dependency on Libc.So
Why Aren't Static Const Floats Allowed
What Is an Opaque Value in C++
Creating a Zip File on Windows (Xp/2003) in C/C++
Get Function Pointer from Std::Function When Using Std::Bind