Install GNU GCC on mac
Asked Answered
M

4

15

I have recently become frustrated with the new clang compiler included with Xcode 5. I was wondering what the best way to install GNU GCC on OS X would be.

Things to consider:

  • I don't want to use MacPorts, fink, homebrew or any other third party package manager.
  • I would like to use the latest GCC, compiled from source, if possible.
  • I need the existing GCC (hardlink to clang) to remain the default, but to easily be able to use GNU GCC when I need to.
  • I would like to avoid modifying the code if at all possible.

EDIT: Success! Using GCC 4.9.2 (with GMP 5.1.3, MPFR 3.1.2, MPC 1.0.2, ISL 0.12.2, and CLooG 0.18.1) I succesfully built GCC. Tips to take from here:

  • Make sure you use ISL and CLooG. Yes, they are optional, but they make a more optimised compiler and I had trouble building without them. Make sure you use ISL 0.12, not the latest version (0.14).
  • If possible, use the standalone Developer Tools, not XCode. (XCode has some buggy headers, and while I didn't notice any issues building GCC I have had issues with other software (e.g. GPG)).
  • I recommend putting all your sources in the gcc directory, rather than building and installing separately (it's faster this way)
  • Make sure you invoke configure with CC=clang CXX=clang++ so GCC knows it isn't being compiled by GCC.
  • Definitely, definitely, definitely, invoke make with -j8, or otherwise the build will take about 4 hours! (With -j8, it took 1 1/3 hours on my Mid-2012 MBP with 8gb RAM.) (make -j8 means make can build 8 threads simultaneously (4 cores + HT), whereas on a 2-core machine you would run make -j4.)

Hope this helps!

Moo answered 16/1, 2014 at 21:43 Comment(6)
You've eliminated the easy solutions - e.g. MacPorts: port -b install gcc48 -universal; port select --set gcc mp-gcc48 - and you would like to compile from source, but that didn't work, and you don't provide an error log or the failed configure options. Do you realise how many things can go wrong building a toolchain? You will need to be a lot more specific about the errors you are encountering.Sociopath
@BrettHale Is this any help: gist.github.com/felixphew/d024c8ea88ed9c2ffd52 ? Also, read the edited question for the full storyMoo
There are no errors in these 14k+ lines of log. It diesn't seem finished though. Is this all you have, or the log somehow got truncated?Schiller
@BrettHale Yes sorry, that log was truncated to 1mb, here is the full log.Moo
looks like the same issue as here. Anyway I would recommend installing from ports and then, if you need it, install from source using the ports gcc one as your bootstrap compiler.Schiller
@n.m. I might try that as a last resort. Thanks!Moo
R
6

The way I do it is:

  1. Download the source for GCC and numerous supporting packages. The instructions are in the gcc-4.x.y/INSTALL/index.html file in the GCC source code, or online at http://gcc.gnu.org/install/.

  2. Use a script to extract the source for GCC and the support libraries into a directory, create the object directory, and run the build.

This is the script I used for GCC 4.8.2:

GCC_VER=gcc-4.8.2
tar -xf ${GCC_VER}.tar.bz2 || exit 1

(
cd ${GCC_VER} || exit

cat <<EOF |
    cloog 0.18.0 tar.gz 
    gmp 5.1.3 tar.xz 
    isl 0.11.1 tar.bz2 
    mpc 1.0.1 tar.gz 
    mpfr 3.1.2 tar.xz
EOF

while read file vrsn extn
do
    (
    set -x
    tar -xf "../$file-$vrsn.$extn" &&
    ln -s "$file-$vrsn" "$file"
    )
done
)

mkdir ${GCC_VER}-obj
cd ${GCC_VER}-obj
../${GCC_VER}/configure --prefix=$HOME/gcc/gcc-4.8.2
make -j8 bootstrap

When that finishes, run the install too. Then add $HOME/gcc/gcc-4.8.2/bin (the name you specify in --prefix plus /bin) to your PATH ahead of /usr/bin.

With a decent MacBook Pro with a 5400 rpm spinning disk, it takes an hour or two to compile everything (using the -j8 option to make), and requires multiple gigabytes of disk space while compiling. SSD is nice when doing this (definitely faster)!


GCC 4.9.0 was released on 2014-04-22. I've installed it using basically the same process, but with CLooG 0.18.1 and ISL 0.12.2 (required updates) and GMP 5.1.3 (and 6.0.0a), MPC 1.0.2 (or 1.0.1) and MPFR 3.1.2 on Mac OS X 10.9.2 Mavericks and an Ubuntu 12.04 derivative. Beware that the gmp-6.0.0a.tar.xz extracts into directory gmp-6.0.0 (not gmp-6.0.0a as you might expect).


Between 2014 and 2017-09-27, I've built GCC versions 4.9.0, 4.9.1, 5.1.0, 5.2.0, 5.3.0, 6.1.0, 6.2.0, 6.3.0, 7.1.0 with only minor variations in the build script shown below for GCC 7.2.0 on macOS Sierra (10.12). The versions of the auxilliary libraries changed reasonably often.


macOS Sierra and High Sierra

On 2017-08-14, I used a minor variant of the script above to build GCC 7.2.0 on macOS Sierra 10.12 (using XCode 8 as the bootstrap compiler). One change is that CLooG doesn't seem to be needed any more (I stopped adding it with GCC 6.2.0). This is my current script:

#!/bin/bash

#export DYLD_LIBRARY_PATH=$(clnpath $(dirname $(dirname $(which g++)))/lib:$DYLD_LIBRARY_PATH)
unset DYLD_LIBRARY_PATH

TAR=/opt/gnu/bin/tar
VER_NUM=7.2.0
GCC_VER=gcc-${VER_NUM}
TGT_BASE=/opt/gcc
TGT_DIR=${TGT_BASE}/v${VER_NUM}
CC=/usr/bin/clang
CXX=/usr/bin/clang++

extract() {
    echo "Extract $1"
    $TAR -xf $1
}

if [ ! -d "$GCC_VER" ]
then extract ${GCC_VER}.tar.xz || exit 1
fi

(
cd ${GCC_VER} || exit

nbncl <<EOF |
    gmp     6.1.2   tar.lz 
    isl     0.16.1  tar.bz2 
    mpc     1.0.3   tar.gz 
    mpfr    3.1.5   tar.xz
EOF

while read file vrsn extn
do
    tarfile="../$file-$vrsn.$extn"
    if [ ! -f "$tarfile" ]
    then echo "Cannot find $tarfile" >&2; exit 1;
    fi
    if [ ! -d "$file-$vrsn" ]
    then
        (
        set -x
        extract "$tarfile" &&
        ln -s "$file-$vrsn" "$file"
        ) || exit 1
    fi
done
)

if [ $? = 0 ]
then
    mkdir ${GCC_VER}-obj
    cd ${GCC_VER}-obj
    ../${GCC_VER}/configure --prefix="${TGT_DIR}" \
        CC="${CC}" \
        CXX="${CXX}"
    make -j8 bootstrap
fi

Make sure your version of tar supports all 4 different compressed file formats (.lz, .gz, .xz, .bz2), but since the standard Mac version of tar does that for me, it'll probably work for you too.

On 2017-09-27, I failed to build GCC 7.2.0 on macOS High Sierra 10.13 (using XCode 9 for the bootstrap compiler) using the same script as worked on Sierra 10.12. The immediate error was a missing header <stack>; I'll need to track down whether my XCode 9 installation is correct — or, more accurately, why it isn't correct since <stack> is a standard header in C++98 onwards. There's probably an easy fix; I just haven't spent the time chasing it yet. (Yes, I've run xcode-select --install multiple times; the fact that I had to run it multiple times because of network glitches may be part of the trouble.) (I got GCC 7.2.0 to compile successfully on 2017-12-02; I don't recall what gymnastics — if any — were required to get this to work.)

Time passes; version numbers increase. However, the basic recipe has worked for me with more recent versions of GCC. I have 7.3.0 (installed 2018-01-2), 8.1.0 (installed 2018-05-02), 8.2.0 (installed 2018-07-26), 8.3.0 (installed 2019-03-01) and now 9.1.0 (installed today, 2019-05-03). Each of these versions was built and installed on the current version of macOS at the time, using the current version of XCode for the bootstrap phase (so using macOS 10.14.4 Mojave and XCode 10.2.1 when building GCC 9.1.0)

Rigging answered 19/1, 2014 at 6:6 Comment(9)
Where did you hear that it needed ISL and CLooG? I did install GMP, MPFR, and MPC, and told ./configure where to find them (not to mention telling them where to find each other during build!). The build goes well for 15 minutes or so, and then complains about a missing directory the first time, and after a make distclean and retrying the entire process it complains about a missing stdio.Moo
ISL and CLooG are mentioned explicitly on the install page linked to. They're semi-optional. I've not had any problems with them. As to why your build is failing, I don't know. When I last built it on this machine (2013-10-21, between 21:35 and 22:13, approximately), I had no problems. Note that I explicitly did not install GMP, MPFR or MPC separately. I've done that in the past and had problems. I let GCC find the correct versions in the source tree and it builds and uses those. That's the 'tip of the day'...do not try to use independently installed versions of the prerequisites.Rigging
How can it build them itself, when ./configure fails unless I tell it where to find them?Moo
The configure process looks to see if there is, for example, a directory mpfr in the gcc-4.8.2 directory, and if that contains the source for an OK version of MPFR. If it is there, it uses that. If it is not, it looks to see if it can find prebuilt code that will work instead. The script shown makes sure that the source is found in the gcc-4.8.2 directory.Rigging
And, just for the record, it looks like I stopped the October build somewhere in stage 3; when I ran 'make' again to check that it was complete, it went off and did a lot of compilation again. So, the timing (which was faster than I remembered) was not just 40 minutes; my 'hour or two' estimate is more nearly accurate.Rigging
By the way, what's with the -j8?Moo
The -j8 means that up to 8 processes can be running at once; it enables parallel builds, and speed things up dramatically.Rigging
OK, this is great, but it didn't work. Still, I guess it's technically what I'm after, so you get the tick.Moo
OK, it works now! Maybe GCC-4.8.2 was buggy, but this build (outlined above) went without a hitch.Moo
M
10

Homebrew now has the GCC package so you can install it with this command:

brew install gcc
Malatya answered 23/2, 2017 at 2:46 Comment(3)
Thanks for your answer, however the point of the question was to compile GCC without external tools or package managers. (Otherwise I would have used MacPorts.)Moo
It's good to have the answer here since this question shows up first on Google Search on the topic.Melidamelilot
Also in order your make files to pick up proper gcc. Don't use the Xcode's Clang's gcc else it will constantly ask to install Command Line Developer Tools. Just use symlink for your make files to find the appropriate gcc. e.g. sudo ln -sf /opt/homebrew/bin/gcc-13 /usr/local/bin/x86_64-linux-gnu-gcc or sudo ln -sf /opt/homebrew/bin/gcc-13 /usr/local/bin/aarch64-linux-gnu-gcc as the case maybePrecession
R
6

The way I do it is:

  1. Download the source for GCC and numerous supporting packages. The instructions are in the gcc-4.x.y/INSTALL/index.html file in the GCC source code, or online at http://gcc.gnu.org/install/.

  2. Use a script to extract the source for GCC and the support libraries into a directory, create the object directory, and run the build.

This is the script I used for GCC 4.8.2:

GCC_VER=gcc-4.8.2
tar -xf ${GCC_VER}.tar.bz2 || exit 1

(
cd ${GCC_VER} || exit

cat <<EOF |
    cloog 0.18.0 tar.gz 
    gmp 5.1.3 tar.xz 
    isl 0.11.1 tar.bz2 
    mpc 1.0.1 tar.gz 
    mpfr 3.1.2 tar.xz
EOF

while read file vrsn extn
do
    (
    set -x
    tar -xf "../$file-$vrsn.$extn" &&
    ln -s "$file-$vrsn" "$file"
    )
done
)

mkdir ${GCC_VER}-obj
cd ${GCC_VER}-obj
../${GCC_VER}/configure --prefix=$HOME/gcc/gcc-4.8.2
make -j8 bootstrap

When that finishes, run the install too. Then add $HOME/gcc/gcc-4.8.2/bin (the name you specify in --prefix plus /bin) to your PATH ahead of /usr/bin.

With a decent MacBook Pro with a 5400 rpm spinning disk, it takes an hour or two to compile everything (using the -j8 option to make), and requires multiple gigabytes of disk space while compiling. SSD is nice when doing this (definitely faster)!


GCC 4.9.0 was released on 2014-04-22. I've installed it using basically the same process, but with CLooG 0.18.1 and ISL 0.12.2 (required updates) and GMP 5.1.3 (and 6.0.0a), MPC 1.0.2 (or 1.0.1) and MPFR 3.1.2 on Mac OS X 10.9.2 Mavericks and an Ubuntu 12.04 derivative. Beware that the gmp-6.0.0a.tar.xz extracts into directory gmp-6.0.0 (not gmp-6.0.0a as you might expect).


Between 2014 and 2017-09-27, I've built GCC versions 4.9.0, 4.9.1, 5.1.0, 5.2.0, 5.3.0, 6.1.0, 6.2.0, 6.3.0, 7.1.0 with only minor variations in the build script shown below for GCC 7.2.0 on macOS Sierra (10.12). The versions of the auxilliary libraries changed reasonably often.


macOS Sierra and High Sierra

On 2017-08-14, I used a minor variant of the script above to build GCC 7.2.0 on macOS Sierra 10.12 (using XCode 8 as the bootstrap compiler). One change is that CLooG doesn't seem to be needed any more (I stopped adding it with GCC 6.2.0). This is my current script:

#!/bin/bash

#export DYLD_LIBRARY_PATH=$(clnpath $(dirname $(dirname $(which g++)))/lib:$DYLD_LIBRARY_PATH)
unset DYLD_LIBRARY_PATH

TAR=/opt/gnu/bin/tar
VER_NUM=7.2.0
GCC_VER=gcc-${VER_NUM}
TGT_BASE=/opt/gcc
TGT_DIR=${TGT_BASE}/v${VER_NUM}
CC=/usr/bin/clang
CXX=/usr/bin/clang++

extract() {
    echo "Extract $1"
    $TAR -xf $1
}

if [ ! -d "$GCC_VER" ]
then extract ${GCC_VER}.tar.xz || exit 1
fi

(
cd ${GCC_VER} || exit

nbncl <<EOF |
    gmp     6.1.2   tar.lz 
    isl     0.16.1  tar.bz2 
    mpc     1.0.3   tar.gz 
    mpfr    3.1.5   tar.xz
EOF

while read file vrsn extn
do
    tarfile="../$file-$vrsn.$extn"
    if [ ! -f "$tarfile" ]
    then echo "Cannot find $tarfile" >&2; exit 1;
    fi
    if [ ! -d "$file-$vrsn" ]
    then
        (
        set -x
        extract "$tarfile" &&
        ln -s "$file-$vrsn" "$file"
        ) || exit 1
    fi
done
)

if [ $? = 0 ]
then
    mkdir ${GCC_VER}-obj
    cd ${GCC_VER}-obj
    ../${GCC_VER}/configure --prefix="${TGT_DIR}" \
        CC="${CC}" \
        CXX="${CXX}"
    make -j8 bootstrap
fi

Make sure your version of tar supports all 4 different compressed file formats (.lz, .gz, .xz, .bz2), but since the standard Mac version of tar does that for me, it'll probably work for you too.

On 2017-09-27, I failed to build GCC 7.2.0 on macOS High Sierra 10.13 (using XCode 9 for the bootstrap compiler) using the same script as worked on Sierra 10.12. The immediate error was a missing header <stack>; I'll need to track down whether my XCode 9 installation is correct — or, more accurately, why it isn't correct since <stack> is a standard header in C++98 onwards. There's probably an easy fix; I just haven't spent the time chasing it yet. (Yes, I've run xcode-select --install multiple times; the fact that I had to run it multiple times because of network glitches may be part of the trouble.) (I got GCC 7.2.0 to compile successfully on 2017-12-02; I don't recall what gymnastics — if any — were required to get this to work.)

Time passes; version numbers increase. However, the basic recipe has worked for me with more recent versions of GCC. I have 7.3.0 (installed 2018-01-2), 8.1.0 (installed 2018-05-02), 8.2.0 (installed 2018-07-26), 8.3.0 (installed 2019-03-01) and now 9.1.0 (installed today, 2019-05-03). Each of these versions was built and installed on the current version of macOS at the time, using the current version of XCode for the bootstrap phase (so using macOS 10.14.4 Mojave and XCode 10.2.1 when building GCC 9.1.0)

Rigging answered 19/1, 2014 at 6:6 Comment(9)
Where did you hear that it needed ISL and CLooG? I did install GMP, MPFR, and MPC, and told ./configure where to find them (not to mention telling them where to find each other during build!). The build goes well for 15 minutes or so, and then complains about a missing directory the first time, and after a make distclean and retrying the entire process it complains about a missing stdio.Moo
ISL and CLooG are mentioned explicitly on the install page linked to. They're semi-optional. I've not had any problems with them. As to why your build is failing, I don't know. When I last built it on this machine (2013-10-21, between 21:35 and 22:13, approximately), I had no problems. Note that I explicitly did not install GMP, MPFR or MPC separately. I've done that in the past and had problems. I let GCC find the correct versions in the source tree and it builds and uses those. That's the 'tip of the day'...do not try to use independently installed versions of the prerequisites.Rigging
How can it build them itself, when ./configure fails unless I tell it where to find them?Moo
The configure process looks to see if there is, for example, a directory mpfr in the gcc-4.8.2 directory, and if that contains the source for an OK version of MPFR. If it is there, it uses that. If it is not, it looks to see if it can find prebuilt code that will work instead. The script shown makes sure that the source is found in the gcc-4.8.2 directory.Rigging
And, just for the record, it looks like I stopped the October build somewhere in stage 3; when I ran 'make' again to check that it was complete, it went off and did a lot of compilation again. So, the timing (which was faster than I remembered) was not just 40 minutes; my 'hour or two' estimate is more nearly accurate.Rigging
By the way, what's with the -j8?Moo
The -j8 means that up to 8 processes can be running at once; it enables parallel builds, and speed things up dramatically.Rigging
OK, this is great, but it didn't work. Still, I guess it's technically what I'm after, so you get the tick.Moo
OK, it works now! Maybe GCC-4.8.2 was buggy, but this build (outlined above) went without a hitch.Moo
B
3

Use a pre-compiled binary specifically for OS X 10.9.x Mavericks:

gcc-4.9

Compiled using source code from the GNU servers.

This contains current versions (4.7 is the stable release) of gfortran (free, open source, GNU Fortran 95 compiler), gcc (GNU C) and g++ (GNU C++) compilers that can perform auto-vectorization (i.e. modify code to take advantage of AltiVec/SSE, automatically) and other sophisticated optimizations like OpenMP. For more information, see this webpage.

Download my binaries, and cd to the download folder. Then gunzip gcc-4.9-bin.tar.gz (if your browser didn't do so already) and then sudo tar -xvf gcc-4.9-bin.tar -C /. It installs everything in /usr/local. You can invoke the Fortran 95 compiler by simply typing gfortran. You will also need to have Apple's XCode Tools installed from the Mac App Store. With XCode 4 or 5 you will need to download the command-line tools as an additional step. You will find the option to download the command-line tools in XCode's Preferences.

On 10.9 Mavericks, you can get the command-line tools by simply typing xcode-select --install.

Burra answered 19/1, 2014 at 6:4 Comment(5)
That may be a solution, but I would ideally like a straight GCC, compiled on my machine, or possibly an official package from GNU. This could be handy as a last resort, though. Thanks!Moo
@felixphew, You're welcome! What exactly is "a straight GCC"? Never heard that one before. o_OFitzpatrick
By "straight GCC" I mean one compiled directly from the latest official source, without modifications or patches.Moo
@felixphew, Maybe you don't realize this, but even the Apple version of GCC (that was included up until 10.9) was not the "official" version; it was their own modified version. And which modifications and/or patches are you referring to with the pre-compiled version I've linked?Fitzpatrick
Yes, I was aware that the Apple GCC was never official, in fact, that is why I tried to do this on my computer even before I upgraded to Mavericks. However, when it didn't work, I just went "oh well" and made do. Now, however, the apple compiler has a few annoying incompatibilities with GCC that I would rather not deal with.Moo
P
0

Homebrew now has the GCC package so you can install it with this command:

brew install gcc

Don't use the Xcode's Clang's gcc else it will constantly ask to install Command Line Developer Tools.

In order your make files to pick up proper homebrew gcc, just use symlink for your make files to find the appropriate gcc. e.g.

sudo ln -sf /opt/homebrew/bin/gcc-13 /usr/local/bin/x86_64-linux-gnu-gcc or

sudo ln -sf /opt/homebrew/bin/gcc-13 /usr/local/bin/aarch64-linux-gnu-gcc

as the case maybe.

Worked successfully on Sonoma 14.1.2

If you have installed make and cmake using homebrew, you need to add the bin paths to the PATH variable. e.g. below we are adding the gnubin path to PATH in fish shell.

set -U fish_user_paths /opt/homebrew/opt/make/libexec/gnubin $fish_user_paths

Precession answered 1/12, 2023 at 16:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.