ago
14
2010

Building GCC [4.6] on MacOSX

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 and gcc 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 ;-)

tags: ,
posted in mac by Tiago Maluta

Follow comments via the RSS Feed | Deixe um comentário | Trackback URL

11 Comments to "Building GCC [4.6] on MacOSX"

  1. Potato wrote:

    During the call to configure for step 4 I get:

    configure: error: gmp.h can’t be found, or is unusable.

    I tried passing CPPFLAGS -I and also –with-gmp-include and neither did it, even though I passed the directory where gmp.h is happily sitting. I don’t get it.

  2. replace wrote:

    try replacing ~/Projects/ with /Users/yourname/Projects …

  3. Tiago Maluta wrote:

    yes. try full path instead. i’ll update the post soon to avoid this kind of error.

  4. Alvyn wrote:

    What does –prefix=$(cd ../install && pwd) means exactly? Can you use some simpler way to write these lines please?

    What I get by entering –prefix=$(cd ../install && pwd) or -–prefix=$(cd ../install && pwd) returns

    configure: error: invalid variable name: `–-prefix’

    configure: error: invalid variable name: `–prefix’

  5. Alvyn wrote:

    Oh… I think I know what that is. Sorry for the last post. I replaced ‘–’ by the minus sign ‘-’ on my keyboard. I guess WordPress replace it automatically.

  6. Alvyn wrote:

    Typo again!

    ‘–’ should be replaced by two minus signs. ‘–’.

  7. Pacman wrote:

    For several months, I’ve tried to get this to work.
    Recently, I found out to change gcc to use gcc-4.2 instead of gcc-4.0; this allows me to compile GMP,MPFR and MPC.
    [rant: why do people ruin a well-working compiler, which works on nearly all platforms?]

    Well, the libraries build. No matter whether I install them or compile them to a different directory, I get the following result:

    checking for the correct version of gmp.h… yes
    checking for the correct version of mpfr.h… yes
    checking for the correct version of mpc.h… yes
    checking for the correct version of the gmp/mpfr/mpc libraries… no
    configure: error: Building GCC requires GMP 4.2+, MPFR 2.3.1+ and MPC 0.8.0+.
    Try the –with-gmp, –with-mpfr and/or –with-mpc options to specify
    their locations. Source code for these libraries can be found at
    their respective hosting sites as well as at
    ftp://gcc.gnu.org/pub/gcc/infrastructure/. See also
    http://gcc.gnu.org/install/prerequisites.html for additional info. If
    you obtained GMP, MPFR and/or MPC from a vendor distribution package,
    make sure that you have installed both the libraries and the header
    files. They may be located in separate packages.

    (This result, I got from following the tutorial on this page; you’ve gotten it to work with these versions, so I must be able to too).
    I’m using a PowerMac G5/DP 2.0 with Mac OS X Leopard 10.5.8 and XCode 3.14. I switched to GCC 4.2.
    As gcc_select is no longer supplied, I have 3 scripts placed in /usr/bin, similar to the following:

    #!/bin/sh
    # file:use_gcc-4.2
    sudo rm cc gcc c++ g++ gcov
    sudo ln -s gcc-4.2 cc
    sudo ln -s gcc-4.2 gcc
    sudo ln -s c++-4.2 c++
    sudo ln -s g++-4.2 g++
    sudo ln -s gcov-4.2 gcov

    I hope my message here will help some people getting their AVR/AVR32/ARM toolchains to compile, but I’m really stuck here.
    (If you can’t get past this level, I can tell you that GCC 4.2.3 compiles fine; it doesn’t need GMP/MPFR/MPC)

  8. Pacman wrote:

    Now I finally got it to work!!

    You need to compile with option ABI=32 and put the GMP, MPFR and MPC sources inside the GCC source folder.
    Eg…
    gcc–4.6.0/
    gmp/
    mpfr/
    mpc/

    ..I do it this way:

    download and extract gcc–4.6.0
    mv gcc–4.6.0 gcc
    download and extract gmp–5.0.1
    mv gmp–5.0.1 gcc/gmp
    download and extract mpfr–3.0.0 (maybe 3.0.1)
    mv mpfr–3.0.1 gcc/mpfr
    download and extract mpc–0.8.2 (maybe 0.9)
    mv mpc–0.8.2 gcc/mpc

    set a variable…
    LIBDIR=$(pwd)/libs
    compile gmp, mpfr and mpc, optionally with ––prefix=$LIBDIR
    compile gcc
    You *will* be able to specify custom locations using ––prefix=$LIBDIR and ––with–*=$LIBDIR

    With this information, you *should* be able to do a successful build of a microcontroller–toolchain.
    (note, I’ve tried changing the minus to a long dash, hoping that it’ll show up a bit better than the minus)

  9. Pacman wrote:

    (WordPress killed my formatting)

    Directories should have been:
    gcc–4.6.0/
    gcc–4.6.0/gmp/
    gcc–4.6.0/mpfr/
    gcc–4.6.0/mpc/

  10. Tiago Maluta wrote:

    @Pacman – thanks for sharing, we all know cross-compiling isn’t easy…, a good article on that http://www.airs.com/blog/archives/492

  11. KimRyungee wrote:

    Hi.
    Thank you for your post.
    It’s really helped.
    BTW, You’ve miskeied on step #5
    –with-mpfr=~/Projects/GCC/libs/install
    Not
    –with-mprf=~/Projects/GCC/libs/install

    Bye~~

Leave Your Comment