Building GCC [4.6] on MacOSX

August 14, 2010

[Warning: This post is a backup recovery from my previous Wordpress blog. All content was automatically converted accessing a MySQL database using a Python script (details). Mostly are in Portuguese but if you are interest I can translate to English. If you found any problem dont’t hesitate to contact me in comments.]

The objective here is describe a didactic way to build GNU GCC on MacOSX. In order to compile GCC you need three libraries: GMP, MPFR and MPC.To organize I usually create folders for each purpose. In this case, three, respectively: source, build and install. [It's not a rule]. My original enviroment is MacOSX 10.6.4 andgcc version 4.2.1 (Apple Inc. build 5659). All files will be on GCC folder, the description below shows:

$ mkdir ~/Projects/GCC # compiler + libs $ mkdir ~/Projects/GCC/libs #gmp, mpfr and mpc $ mkdir ~/Projects/GCC/libs/files # downloaded files $ mkdir ~/Projects/GCC/libs/install # store libs objects and include

Step #1 - Download

$ cd ~/Projects/GCC/libs/files $ wget ftp://ftp.gmplib.org/pub/gmp-5.0.1/gmp-5.0.1.tar.bz2 $ wget http://www.mpfr.org/mpfr-current/mpfr-3.0.0.tar.bz2 $ wget http://www.multiprecision.org/mpc/download/mpc-0.8.2.tar.gz

Step #2 - Unpack

$ cd ~/Projects/GCC/libs/ $ tar jxvf files/gmp-5.0.1.tar.bz2 $ tar jxvf files/mpfr-3.0.0.tar.bz2 $ tar zxvf files/mpc-0.8.2.tar.gz

Step #3 - Build: GMP

$ cd ~/Projects/GCC/libs/

$ mkdir gmp-build $ cd gmp-build $ ../gmp-5.0.1/configure --prefix=$(cd ../install && pwd) $ make install

Note 1: I’m using with ABI=64 Note 2 : Maybe you’ll get some unresolved symbols to GMP and MPFR on linking time, never mind.

Step #4 - Build: MPFR

$ cd ~/Projects/GCC/libs/

$ mkdir mpfr-build $ cd mpfr-build $ ../mpfr-3.0.0/configure --prefix=$(cd ../install && pwd) --with-gmp=~/Projects/GCC/libs/install $ make install

Note: Before prefix and with-xxx there are two hyphen (not one as showed). It’s an Wordpress issue, I don’t know how to avoid concatenation.

Step #5 - Build: MPC

$ cd ~/Projects/GCC/libs/

$ mkdir mpc-build $ cd mpc-build $ ../mpc-0.8.2/configure --prefix=$(cd ../install && pwd) --with-gmp=~/Projects/GCC/libs/install --with-mprf=~/Projects/GCC/libs/install $ make install

Problems? If something goes wrong...

../../mpc-0.8.2/src/mpc.h:25:17: error: gmp.h: No such file or directory ../../mpc-0.8.2/src/mpc.h:26:18: error: mpfr.h: No such file or directory

I fixed by adding symbolic links:

$ cd ~/Projects/GCC/libs/mpc-0.8.2/src/ $ ln -s ../../install/include/mpf2mpfr.h . $ ln -s ../../install/include/mpfr.h . $ ln -s ../../install/include/gmp.h .

Step #6 - Download & Build GCC (~4.6)

I used GCC from git (fda0037801fb258a2191aba59e1e9f0df019e3b6) and I don't know if it will work on newer versions. You'll have to try. Sorry. [Howto:GitMirror]. Use git checkout to specify one commit.

$ cd ~/Projects/GCC/

$ git clone git://gcc.gnu.org/git/gcc.git

$ mkdir build $ mkdir install

$ cd build $ ../gcc/configure --prefix=$(cd ../install/ && pwd) --with-gmp=/Users/maluta/Projects/GCC/libs/install --with-mpfr=/Users/maluta/Projects/GCC/libs/install --with-mpc=/Users/maluta/Projects/GCC/libs/install --disable-checking --enable-werror --enable-languages=c

$ make $ make install

Step #7 - Test

The binary files will be placed in ~/Projects/GCC/install/bin

$ ~/Projects/GCC/install/bin $ ./gcc -v

Using built-in specs. COLLECT_GCC=./gcc COLLECT_LTO_WRAPPER=/Users/maluta/Projects/GCC/install/libexec/gcc/x86_64-apple-darwin10.4.0/4.6.0/lto-wrapper Target: x86_64-apple-darwin10.4.0 Configured with: ../gcc/configure --prefix=/Users/maluta/Projects/GCC/install --with-gmp=/Users/maluta/Projects/GCC/libs/install --with-mpfr=/Users/maluta/Projects/GCC/libs/install --with-mpc=/Users/maluta/Projects/GCC/libs/install --disable-checking --enable-werror --enable-languages=c Thread model: posix gcc version 4.6.0 20100508 (experimental) (GCC)

Although I focused on OSX build/installation the steps described are the same to any architecture. Build an compiler - especially a cross-compiler (no that case) - demands time and patience to understand many particularities.

Enjoy ;-)