Compiling g++-4.1 on Ubuntu Natty Narwhal

I recently upgraded my work machine to the latest Ubuntu version (natty narwhal, 11.<something>). By and large this went smoothly. However, we require gcc/g++-4.1 , and I had neglected to verify that this was available after the upgrade.

It wasn’t.

So, after a few abortive attempts to install packages I found on the web and such, I determined that what I really needed to do was just build gcc from source. I’ve done this a number of times in the past, so I knew what I was getting into, and by-and-large the process was very straightforward. I had to patch the gcc build files in a few places, though, so I figured I should document the process to save me and others time in the future.

NOTE: This is on an amd64 machine, building a compiler for the same platform. YMMV.

Install a few packages

For the build to work, you’ll need to install a few other packages that provide headers files, etc. The packages you’ll need are:

  • linux-header-<kernel-version>
  • gcc-multilib

(There may be others, and I’ll try to update this list as I hear about them.)

Make MULTILIB look in the right directories

In gcc/config/i386/t-linux64 you need to correct the directories used for MULTILIB stuff. Change line 9 from:
MULTILIB_OSDIRNAMES = ../lib64 ../lib
to
MULTILIB_OSDIRNAMES = ../lib ../lib32

Change the “version” string that the build expects from ld

The gcc build parses the output of “ld –version”. Unfortunately, ubuntu decided to change that output, causing gcc to think that the ld version is really old. In libstdc++-v3/configure change line 8284 from:
sed -e 's/GNU ld version \([0-9.][0-9.]*\).*/\1/'`
to
sed -e 's/GNU ld (GNU Binutils for Ubuntu) \([0-9.][0-9.]*\).*/\1/'`

The full patch

You can download the patch from pastebin.

Be aware that this patch is only against gcc-core and gcc-g++, not against the entire gcc suite.

Step-by-step

These steps should more-or-less do everything you need to download, patch, build, and install gcc/g++-4.1. Everything will get installed to /usr/local/gcc-4.1/[bin,lib], and the programs will have the suffix “-4.1”.

wget ftp://ftp.nluug.nl/mirror/languages/gcc/releases/gcc-4.1.2/gcc-core-4.1.2.tar.bz2
wget ftp://ftp.nluug.nl/mirror/languages/gcc/releases/gcc-4.1.2/gcc-g++-4.1.2.tar.bz2
tar jxvf gcc-core-4.1.2.tar.bz2
tar jxvf gcc-g++-4.1.2.tar.bz2
<download patch from pastebin>
cd gcc-4.1.2
patch -p1 < ../FKkiciZz.txt
./configure --prefix=/usr/local/gcc-4.1 --program-suffix=-4.1
make
make install
...set up symlinks to /usr/local/gcc-4.1/bin/g++-4.1, etc...
Advertisements

,

  1. #1 by Sebastian Schuberth on 2011/04/28 - 14:27

    I’m in the same situation, thanks for the tip. Would you mind making the patch available as a download, too? This avoids possible whitespace / wrapping errors when copying & pasting from the browser.

    Or even better, is it easily possible to build source or binary packages from your results that you could offer for download?

    • #2 by abingham on 2011/04/28 - 14:32

      Yeah, I apologize for the sorry state of the post! I tossed it together in a hurry and moved on, but I’ll try to put together something a bit more useful soon. Hope it helped anyway 🙂

    • #3 by abingham on 2011/04/29 - 07:22

      OK, I’ve put the raw patch up on pastebin. I probably won’t be creating any sort of package, though. I just don’t have the time or patience for it right now…sorry.

      • #4 by Sebastian Schuberth on 2011/04/29 - 08:05

        Thanks! As I just realized, the dist upgrade to 11.04 did not remove the GCC 4.1 that I had installed on 10.10 (simply by temporarily adding the 10.04 “Lucid” repository), so I can save the time to compile GCC 4.1 from sources 🙂

  2. #5 by Fusheng Han on 2011/09/16 - 05:31

    make[4]: 正在进入目录 `/home/hanfoo/下载/gcc-4.1.2/x86_64-unknown-linux-gnu/libmudflap’
    if /bin/sh ./libtool –mode=compile /home/hanfoo/downloads/gcc-4.1.2/host-x86_64-unknown-linux-gnu/gcc/xgcc -B/home/hanfoo/downloads/gcc-4.1.2/host-x86_64-unknown-linux-gnu/gcc/ -B/usr/local/gcc-4.1/x86_64-unknown-linux-gnu/bin/ -B/usr/local/gcc-4.1/x86_64-unknown-linux-gnu/lib/ -isystem /usr/local/gcc-4.1/x86_64-unknown-linux-gnu/include -isystem /usr/local/gcc-4.1/x86_64-unknown-linux-gnu/sys-include -DHAVE_CONFIG_H -I. -I../.././libmudflap -I. -Wall -ffunction-sections -fdata-sections -O2 -g -O2 -MT mf-hooks1.lo -MD -MP -MF “.deps/mf-hooks1.Tpo” -c -o mf-hooks1.lo ../.././libmudflap/mf-hooks1.c; \
    then mv -f “.deps/mf-hooks1.Tpo” “.deps/mf-hooks1.Plo”; else rm -f “.deps/mf-hooks1.Tpo”; exit 1; fi
    /home/hanfoo/downloads/gcc-4.1.2/host-x86_64-unknown-linux-gnu/gcc/xgcc -B/home/hanfoo/downloads/gcc-4.1.2/host-x86_64-unknown-linux-gnu/gcc/ -B/usr/local/gcc-4.1/x86_64-unknown-linux-gnu/bin/ -B/usr/local/gcc-4.1/x86_64-unknown-linux-gnu/lib/ -isystem /usr/local/gcc-4.1/x86_64-unknown-linux-gnu/include -isystem /usr/local/gcc-4.1/x86_64-unknown-linux-gnu/sys-include -DHAVE_CONFIG_H -I. -I../.././libmudflap -I. -Wall -ffunction-sections -fdata-sections -O2 -g -O2 -MT mf-hooks1.lo -MD -MP -MF .deps/mf-hooks1.Tpo -c ../.././libmudflap/mf-hooks1.c -fPIC -DPIC -o .libs/mf-hooks1.o
    In file included from ../.././libmudflap/mf-hooks1.c:58:
    /usr/include/unistd.h:275: error: two or more data types in declaration specifiers

    I got this error message in my compiling of gcc-4.1 on ubuntu 11.04.
    Could you give me a hand?

    • #6 by abingham on 2011/09/16 - 06:23

      That’s not much to go on. You’re probably better off looking for an answer on the gcc mailing list, or maybe on an ubuntu-related support list. Sorry 😦

  3. #7 by Yerman Avila on 2011/09/29 - 17:28

    hi all, I’m need to compile systemc using gcc-4.1 and g++-4.1 but I have ubuntu 11.04 i386 and I had not been able to install gcc-4.1 so far.

    I’m need to use for the compilation

    CXX=g++ (version)
    CC=gcc (version)

    My question is: how to create the symlinks for g++? it isn’t clear for me, can you help me?

  4. #8 by Yerman Avila on 2011/09/30 - 15:41

    hi, I find a solution, can you tell me if it is true?

    $ ln -s /usr/local/gcc-4.1/bin/gcc-4.1 /usr/bin/gcc-4.1
    $ ln -s /usr/local/gcc-4.1/bin/g++-4.1 /usr/bin/g++-4.1

    • #9 by abingham on 2011/09/30 - 18:19

      Yes, that looks like it will work just fine. It might be more “natural” if you used /usr/local/bin/gcc-4.1 and /usr/local/bin/g++-4.1, but both will work.

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: