How to build gcc on MacOS X 10.6 (Snow Leopard)
(c) Tommy Olsson, Department of Computer Science, Linköping University
With MaxOS X 10.6 gcc is included, but with MacOS X 10.7 (Lion) we have only LLVM with a gcc front-end. Below are instructions for building and installing gcc on MacOS X 10.6. Note: The installation described below will not interfer with the default installation.
In order to compile GCC you need three arithmetic libraries:
GMP GNU Multiple Precision Arithmetic Library (http://gmplib.org/)
MPFR GNU Multiple-Precision Floating-point Library with correct Rounding (http://www.mpfr.org/)
MPC Library for arithmetic of complex numbers with arbitrarily high precision and correct rounding of the result (http://www.multiprecision.org/)
You also need Xcode, an integrated development environment, installed. Version 3 can be downloaded frre of charge from http://developer.apple.com/xcode/, if you join the iOS Developer University Program.
Xcode includes GCC, where supported languages are C, C++, Objective-C, and Objective-C++. Version 4 of Xcode is also available, but cannot be downloaded freely.
In the examples below the installation is made in a user's (tao) home directory, in a subdirectory Projects/GCC. Of course, at least parts of the build/install process could be implemented with a script.
If no new versions of GMP, MPFR, or MPC are released or required, start from step 7, Download GCC.
1. Create the following directories
$ mkdir /Users/tao/Projects $ mkdir /Users/tao/Projects/GCC $ mkdir /Users/tao/Projects/GCC/libs $ mkdir /Users/tao/Projects/GCC/libs/files $ mkdir /Users/tao/Projects/GCC/libs/install
2. Download the required arithmetic libraries
These versions were the ones available on March 27, 2012:
http://gmplib.org/ gmp-5.0.4.tar.bz2 http://www.mpfr.org/ mpfr-3.1.0.tar.b2z http://www.multiprecision.org/ mpc-0.9.tar.gz
Put the downloaded files in directory /Users/tao/Projects/GCC/libs/files. Some libraries are available in several compression formats (gzip, zip, bzip2, xz).
3. Unpack the downloaded files
$ cd /Users/tao/Projects/GCC/libs $ tar jxvf files/gmp-5.0.4.tar.bz2 $ tar jxvf files/mpfr-3.1.0.tar.bz2 $ tar zxvf files/mpc-0.9.tar.gz
This will create the following directories:
/Users/tao/Projects/GCC/libs/gmp-5.0.4 /Users/tao/Projects/GCC/libs/mpfr-3.1.0 /Users/tao/Projects/GCC/libs/mpc-0.9
If a library need to be re-installed, it may be necessary to give the command 'make distclean' in the source directory.
4. Build, install, and check GMP
$ cd /Users/tao/Projects/GCC/libs $ mkdir gmp-build $ cd gmp-build $ ../gmp-5.0.4/configure --prefix=$(cd ../install && pwd) $ make install $ make check
5. Build, install, and check MPFR
$ cd /Users/tao/Projects/GCC/libs
$ mkdir mpfr-build
$ cd mpfr-build
$ ../mpfr-3.1.0 /configure --prefix=$(cd ../install && pwd) \
--with-gmp=/Users/tao/Projects/GCC/libs/install
$ make install
$ make check
6. Build, install, and check MPC
$ cd /Users/tao/Projects/GCC/libs
$ mkdir mpc-build
$ cd mpc-build
$ ../mpc-0.9/configure --prefix=$(cd ../install && pwd) \
--with-gmp=/Users/tao/Projects/GCC/libs/install \
--with-mpfr=/Users/tao/Projects/GCC/libs/install
$ make install
$ make check
The arithmethic libraries are now installed. To use them, you need to make them available for different tools, e.g. preprocessor, compiler, and linker. When building GCC below, this is done with configure options.
GMP, MPFR, and MPC are installed in the following directories:
/Users/tao/Projects/GCC/libs/install/include /Users/tao/Projects/GCC/libs/install/lib /Users/tao/Projects/GCC/libs/install/share
7. Download GCC
GCC can be downloaded from http://gcc.gnu.org/, or from a mirror site.
Create a directory for the file(s) to be downloaded:
$ cd /Users/tao/Projects/GCC $ mkdir /Users/tao/Projects/GCC/files
Unpack the downloaded file (gcc-4.7.0.tar.gz):
$ cd /Users/tao/Projects/GCC $ tar zxvf files/gcc-4.7.0.tar.gz
This will create the following directory:
/Users/tao/Projects/GCC/gcc-4.7.0
8. Build and install GCC
The working directory is /Users/tao/Projects/GCC/build. The top level installation directory is /Users/tao/Projects/GCC/install, which is set with option '--prefix' to the 'configure' command. Compilers and runtime libraries are built for C, Objective-C, C++, and Objective-C++. This will take about one hour.
$ cd /Users/tao/Projects/GCC
$ mkdir install
$ mkdir build
$ cd build
$ ../gcc-4.7.0/configure --prefix=$(cd ../install && pwd) \
--with-gmp=/Users/tao/Projects/GCC/libs/install \
--with-mpfr=/Users/tao/Projects/GCC/libs/install \
--with-mpc=/Users/tao/Projects/GCC/libs/install \
--disable-checking --enable-werror \
--enable-languages=c,objc,c++,obj-c++
$ make
$ make install
9. Test GCC
The binary files are placed in ~/Projects/GCC/install/bin, move into that directory and test gcc:
$ cd /Users/tao/Projects/GCC/install/bin $ ./gcc -v
This will print information about the installment.
GCC is now installed in the following directories:
/Users/tao/Projects/GCC/install/bin /Users/tao/Projects/GCC/install/include /Users/tao/Projects/GCC/install/lib /Users/tao/Projects/GCC/install/lib/i386 /Users/tao/Projects/GCC/install/libexec /Users/tao/Projects/GCC/install/share
These directories are not searched by default by preprocessor, compilers or linker.
Uninstalling GCC
There is no 'make uninstall' command. Since GCC is built in a separate dirctory, 'install', just remove the files in 'install' when no longer needed.
To rebuild, remove all files in directory 'build', and redo from 'configure'.
GCC environment variables
Unix environment variables can be handled with a property list file named ~/.MacOSX/environment.plist. This directory and file typically does not exist, so you can be quite sure to have to create them yourself. The property list file environment.plist can be edited using a text editor or /Developer/Applications/Utilities/PropertyListEditor.app. Whenever you modify environment.plist, you have to log out and then log in again.
If you are working in an xterm and use bash, you can set environment variables
in the ~/.bashrc file:
PATH="/Users/tao/Projects/GCC/install/bin:$PATH" export C_INCLUDE_PATH="/Users/tao/Projects/GCC/install/include:/Users/tao/Projects/GCC/libs/install/include" export CPLUS_INCLUDE_PATH="/Users/tao/Projects/GCC/install/include:/Users/tao/Projects/GCC/libs/install/include" export LD_LIBRARY_PATH="/Users/tao/Projects/GCC/install/lib:/Users/tao/Projects/GCC/install/lib/i386:/Users/tao/Projects/GCC/libs/install/lib" export DYLD_LIBRARY_PATH="/Users/tao/Projects/GCC/install/lib:/Users/tao/Projects/GCC/install/lib/i386:/Users/tao/Projects/GCC/libs/install/lib"
Page responsible: The UPP group
Last updated: 2012-08-28
