Ehren's Blog

Building GCC from trunk

Posted in Seneca by ehren on September 20, 2009

I’ve recently been assigned a project for DPS909 that involves extending gcc to support an always nonzero function attribute.

Originally, I had expressed some interest in one of the potential gdb projects listed on the wiki, but after being introduced to Taras Glek by David Humphrey on #static, I was informed that the code base for gdb is not exactly the prettiest of the open source world.

I’d like to return shortly with a more conceptual overview of my bug including (hopefully) an overview of how function attributes are implemented in gcc, but for now I have to complete the zeroith step: building gcc from source.

Luckily the GNU Project has put together an excellent series of documents explaining the process, which I will follow very closely. My platform will be a newly installed copy of Debian Testing (Squeeze) i686 on an 8 or 9 year old Dell Dimension.

The first step is getting the sources, so I’ll need svn:

d8200:/home/ehren# aptitude install subversion

Testing that it works:

ehren@d8200:~$ svn ls svn://gcc.gnu.org/svn/gcc
branches/
emptydir/
hooks/
tags/
trunk/

It works! Now let’s check out trunk:

ehren@d8200:~$ mkdir gcc
ehren@d8200:~$ cd gcc
ehren@d8200:~$ svn checkout svn://gcc.gnu.org/svn/gcc/trunk srcdir

Once it’s pulled in the sources we’ll be ready to configure the build. The documents configuration section is somewhat unclear about the proper directory structure for the build but here’s the layout I created:

ehren@d8200:~/gcc$ ls
objdir srcdir dist

srcdir contains the svn checkout, dist contains the “top level installation directory” where the tools will eventually be placed. This directory must be specified (see below), otherwise the tools will be placed in /usr/local. objdir will include our unlinked binaries (I guess) and is the directory where we’ll issue the make commands.

Anyway, I’m getting ahead of myself. As mentioned, we must first specify our top level installation directory (the docs recommend that $HOME or an absolute path should be used over ~):

ehren@d8200:~/Development$ cd objdir/
ehren@d8200:~/Development/objdir$ ../srcdir/configure --prefix=/home/ehren/gcc/dist

uh oh… looks like I should have read the prerequisites more carefully:

checking for correct version of gmp.h... no
checking for the correct version of mpc.h... no
configure: error: Building GCC requires GMP 4.2+ and MPFR 2.3.2+.
Try the --with-gmp and/or --with-mpfr options to specify their locations.
Copies of these libraries' source code 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 and/or MPFR 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.

A quick search of the repositories finds the correct packages however:

d8200:/home/ehren# aptitude install libgmp3c2 libmpfr-dev

Rerunning the above command:

ehren@d8200:~/gcc/objdir$ ../srcdir/configure --prefix=/home/ehren/gcc/dist/
...
configure: creating ./config.status
config.status: creating Makefile

Ok, we’re ready to make!

ehren@d8200:~/gcc/objdir$ make

And now we play the waiting game.

4+ hours later make fails with something along the lines of “cannot find jar … cannot find zip”.
Argh, I really should have read the prerequisites more carefully. Luckily make will pick up where it left off.

So, after a quick aptitude install fastjar zip we’re back on track:

ehren@d8200:~/gcc/objdir$ make

Another 3-4 hours later:

...
make[1]: Leaving directory `/home/ehren/gcc/objdir'
ehren@d8200:~/gcc/objdir$

Woohoo! We are now ready for the install:

ehren@d8200:~/gcc/objdir$ make install

Within a few minutes make exits along with this helpful note:

Libraries have been installed in:
/home/ehren/gcc/dist/lib

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.

Here’s our “top level install directory”:

ehren@d8200:~/gcc/dist$ ls
bin include lib libexec share

We’re finished, but can we compile something with the compiler we compiled? Let’s see:

ehren@d8200:~$ cat hello.c
#include <stdio.h>

int main(void) {
    printf("hello world!\n");
}

ehren@d8200:~$ gcc/dist/bin/gcc hello.c
ehren@d8200:~$ ./a.out
hello world!
ehren@d8200:~$

I’m not linking against the new libraries, but it works (and it only took 8 hours). Oh yeah!

edit (Dec 19, 09): I get quite a few hits on this page so I cleaned up the names I’ve used for the directory structure to make this a bit less confusing. Also, if you don’t want to spend 8 hours on a build I would suggest adding the following to your configure options:

--disable-bootstrap CFLAGS="-g3 -O0" --enable-languages=c,c++

Advertisements

3 Responses

Subscribe to comments with RSS.

  1. David Humphrey said, on September 20, 2009 at 9:32 pm

    Great to see this! About those build times…we have really fast build boxes you guys can use remotely for this course. I’ll show you later this week, and you should be able to get that build time down to under 20 mins.

    • ehren said, on September 20, 2009 at 11:43 pm

      Sounds good. I’d hope subsequent builds on this machine would take less time, but I suppose any file worth modifying will have a tonne of dependencies…

  2. Giridhar said, on September 19, 2011 at 2:00 pm

    When I built gcc on arm 64 bit machine I could only get 64bit libraries, i.e. when I tried ‘-m32’ options I’m getting this message
    /usr/bin/ld: skipping incompatible /usr/local/gcc452/lib/gcc/x86_64-unknown-linux-gnu/4.5.2/libgcc.a when searching for -lgcc
    /usr/bin/ld: cannot find -lgcc
    /usr/bin/ld: cannot find -lgcc_s
    collect2: ld returned 1 exit status
    because it has only 64bit libraries, is there a config option for building 32bit libraries aswell?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: