iEFdev

Code, Computers & Random Junk

Installing GCC in OS X

I recently installed GCC 4.9.2 in OS X. The version in Xcode &/or the separate “cli-tools” package is not the latest. I think it’s a version of 4.2. And some programs you install/compile your self needs a later version. So, here are my “notes” - how I did it. Assuming you already have (Xcode &/or) CLI-tools from Apple installed…

2015-04-21: Updated the post to match my latest install.

Dependencies

In order to compile GCC you’ll first need to install a few libraries.

To prepare, setup an enviroment to work from. Example:

mkdir -pv ~/.builds/GCC/{4.9,gmp,mpfr,mpc,libiconv,isl,cloog}

Install

When I installed this I mainly followed this guide, and also looked a little it at this one. Both examples are installing everything into the same directory, but I have put everything in /usr/local and then gcc-4.9.2 in its own directory. The reason for that is because it more simple to update and maintain. To upgrade gcc you can just throw the dir and create a new one, and the libraries…. I wanted them in /usr/local since other programs can be configured with --with-gmp, for example. So, just to keep it a little bit of standard with /usr/local doesn’t hurt.

All installs will be made from a separate build dir… It’s easier that way if one needs to start over.


Libiconv

Download the source and signature of the latest version. At the moment: 1.14

Verify/unpack the package into: ~/.builds/GCC/libiconv/. Then:

mkdir -pv ~/.builds/GCC/libiconv/libiconv-1.14_build
cd ~/.builds/GCC/libiconv/libiconv-1.14_build

I usually start with putting out a help-file for reference and to see all the configure options etc. To do so:

../libiconv-1.14/configure --help > ../libiconv_help.txt

Build and install (prefix defaults to: /usr/local):

../libiconv-1.14/configure
make -j5
sudo make install

This one is used later in the gcc configure line: --with-libiconv-prefix=/usr/local

A note on make -j5: When you have a multicore CPU, let say a “quadcore”, you have 4 cores, but they are virtually 8. By using -j N you compile the code faster by using N cores. So, if you have 4 (eg 8). To take the number of cores + 1 seems to be a good number.

GMP

gmp, mfpr, mpc needs to be installed in that order.

Download the source and signature of the latest version. At the moment: 6.0.0a

Verify/unpack the package into: ~/.builds/GCC/gmp/. Then:

mkdir -pv ~/.builds/GCC/gmp/gmp-6.0.0_build
cd ~/.builds/GCC/gmp/gmp-6.0.0_build
../gmp-6.0.0/configure --help > ../gmp_help.txt

To configure/install:

../gmp-6.0.0/configure --prefix=/usr/local --enable-cxx
make -j5
sudo make install

MPFR

Download the source and signature of the latest version. At the moment: 3.1.2

Verify/unpack the package into: ~/.builds/GCC/mpfr/. Then:

mkdir -pv ~/.builds/GCC/mpfr/mpfr-3.1.2_build
cd ~/.builds/GCC/mpfr/mpfr-3.1.2_build
../mpfr-3.1.2/configure --help > ../mpfr_help.txt

To configure/install:

../mpfr-3.1.2/configure --prefix=/usr/local --with-gmp=/usr/local
make -j5
sudo make install

MPC

Download the source and signature of the latest version. At the moment: 1.0.2

Verify/unpack the package into: ~/.builds/GCC/mpc/. Then:

mkdir -pv ~/.builds/GCC/mpc/mpc-1.0.2_build
cd ~/.builds/GCC/mpc/mpc-1.0.2_build
../mpc-1.0.2/configure --help > ../mpc_help.txt

To configure/install:

../mpc-1.0.2/configure --prefix=/usr/local --with-gmp=/usr/local --with-mpfr=/usr/local
make -j5
sudo make install

Isl and Cloog

Download the source and signature. They are in the “gcc infrastructure” (ftp link). Some isl versions seems to break the install, so go with isl-0.12.2 and cloog-0.18.1

Verify/unpack the packages into: ~/.builds/GCC/isl/ and ~/.builds/GCC/cloog/. Then:

mkdir -pv ~/.builds/GCC/isl/isl-0.12.2_build
cd ~/.builds/GCC/isl/isl-0.12.2_build
../isl-0.12.2/configure --help > ../isl_help.txt

To configure/install:

../isl-0.12.2/configure --prefix=/usr/local --with-gmp-prefix=/usr/local
make -j5
sudo make install

…and cloog.

mkdir -pv ~/.builds/GCC/cloog/cloog-0.18.1_build
cd ~/.builds/GCC/cloog/cloog-0.18.1_build
../cloog-0.18.1/configure --help > ../cloog_help.txt

../cloog-0.18.1/configure --prefix=/usr/local --with-gmp-prefix=/usr/local --with-isl-prefix=/usr/local
make -j5
sudo make install

GCC

Now everything’s installed seperately into /usr/local. And, if everything went fine - it’s time for GCC.

Download the source and signature of the latest version. At the moment: 4.9.2

Verify/unpack the package into: ~/.builds/GCC/4.9/. Then:

mkdir -pv ~/.builds/GCC/4.9/gcc-4.9.2_build
cd ~/.builds/GCC/4.9/gcc-4.9.2_build
../gcc-4.9.2/configure --help > ../gcc_help.txt

To configure/install:

../gcc-4.9.2/configure \
--disable-nls \
--enable-checking=release \
--enable-languages=c,c++,fortran,objc,obj-c++ \
--enable-shared \
--prefix=/usr/gcc-4.9.2 \
--program-suffix=-4.9.2 \
--with-diagnostics-color=auto \
--with-gmp=/usr/local \
--with-mpfr=/usr/local \
--with-mpc=/usr/local \
--with-gxx-include-dir=/usr/gcc-4.9.2/include/c++/4.9.2 \
--with-libiconv-prefix=/usr/local \
--with-isl=/usr/local \
--with-cloog=/usr/local

make -j5
sudo make install

The make process of GCC is very time consuming. It will take a while. It’s a big program. Using --disable-nls will make it less time consuming when not installing any native languages.

Also, make sure you have enough free space on your disk. The build directory will expand to about ~2.7GB. The installed folder (/usr/gcc-4.9.2) when it’s done is about ~580MB.


If you end up with an error that halts the process, you’ll need to find that and fix it. It’s very hard to tell what to do since every error is your error and depends on you machine. The Duck is you friend.

Post Install

All programs have “-4.9.2” in their name, so symlink the ones you want/need. Example:

cd /usr/gcc-4.9.2/bin
sudo ln -s gcc{-4.9.2,}
sudo ln -s g++{-4.9.2,}
sudo ln -s c++{-4.9.2,}
sudo ln -s cpp{-4.9.2,}
sudo ln -s gcov{-4.9.2,}
sudo ln -s gfortran{-4.9.2,}

And if you don’t want clang to be the default CC:

sudo ln -s gcc-4.9.2 cc

That will make GCC the default.


Now when it’s installed you’ll need to add the location to PATH. Add it you .bashrc, .bash_profile or what file you use. Example:

# GCC
_GCC=/usr/gcc-4.9.2/bin

However, another perhaps better way is to symlink gcc-4.9.2 to just gcc

cd /usr && sudo ln -sf gcc-4.9.2 gcc

….and then use:

_GCC=/usr/gcc/bin

The reason for that is when you upgrade later to another version - programs compiled with /usr/gcc-4.9.2/bin might want that later when looking for a library or something. I just experienced that when updating imagick on PHP and ImageMagick was compiled against my last install, and therefor reported a missing lib.

Put it in PATH:

# PATH
export PATH="$_GCC:/usr/local/bin:/usr/local/sbin:$PATH"

And reload the file (I use .bashrc):

. ~/.bashrc

# Using: . (dot) is the same as using: source

To test and verify:

which gcc g++

To see all versions (including Apple’s)

type -a gcc g++

You can now remove the build folder (~/.build/GCC) and get you GB’s back.

To test it Live… Try to install something simple, not too difficult, like wget »» is quite easy to install/update.

Notes

This is maybe not how it should or usually is done. It’s just how I did it. You can follow the guide, or keep/use it as reference if you find it useful.

Since I don’t use the latest OS X but I still want to have an updated GCC. I’m not doing any C programming, but I need it to install things. So, this is my compiler now.

Actually, I’ve got less errors/warnings when compiling stuff since upgrading to GNU GCC.


Happy hacking!

Comments