Installation of gcc on a user account

The new gcc 4.9 out now, I decided to give it a try on the FreeBSD server my account is on. Of course, no way to convince the admins to have it as default, they’re so conservative they still keep 4.2.1. So the only way is to build it within the user account, and there are some technicalities about it that are worth a note.

The gcc installation process is fairly well documented on the page http://gcc.gnu.org/install/. Download the source, check the signature, unpack. There is a bunch of dependencies mentioned in the Prerequisites, though: GMP, MPFR, MPC, ISL, CLooG. I decided to use the feature by which the libraries are built automatically during the build of gcc if they are found in the respective subdirectories of gcc’s source. So, iterate download, check signature, unpack. The plan was to keep the source in path $HOME/gcc/gcc-4.9.0-src, use path $HOME/gcc/gcc-4.9.0-obj as the build directory, and install to $HOME/gcc/gcc-4.9.0. No global install, not even account-global. So I changed dir to $HOME/gcc/gcc-4.9.0-obj and tried to issue ../gcc-4.9.0-src/configure --prefix=$HOME/gcc/gcc-4.9.0 --enable-version-specific-runtime-libs That, however, triggered a strange error about MPFR not being able to use GMP. It turns out that the former is dependent on the latter, and the automagic all-together build of gcc is not prepared for that. But it can be solved, you just need to say where GMP will be by the time it is needed:

../gcc-4.9.0-src/configure --prefix=$HOME/gcc/gcc-4.9.0 --enable-version-specific-runtime-libs --with-gmp-include=$HOME/gcc/gcc-4.9.0-obj/gmp/ --with-gmp-lib=$HOME/gcc/gcc-4.9.0-obj/gmp/.libs

After that, gmake runs fine, but you’ll need to find something to do before it finishes, and I mean a longer walk rather than a quick coffee. After gmake install, a last thing to be done before the compiler can be used is the setup of the directories that are going to be searched for shared libraries. If you don’t do this, you will end up with your 4.9.0 compiled program trying to use the systemwide 4.2.1 libstdc++. Not good, if it works at all. From what I read, this can be accomplished in two ways. One is, in a sense, global, since it consists in setting up the LD_LIBRARY_PATH, which would change the default path not only for the programs that were built with the new complier, but for everything, which is not what we wanted, see for instance this text: Why LD_LIBRARY_PATH is bad. There are two ways out of this problem: first, use wrapper script that will set it up for every program that you want, call your program, and then clean it up, but oh, this is ugly, and leads to more problems mentioned in the article above, and second, hardcode the path into the executable at link time. This is done with the -rpath linker option, and can be given on-demand at each invocation of ld or of gcc (through -Wl,-rpath,destdir), or it can be set up in the specs file of the particular gcc version, so that all compilations done with it will get the hardcoded path automatically. There is a document Installing compilers on a Linux system that contains a script to do this (see also this page: http://www.math.ttu.edu/~kelong/Linux/develHints.html), but unfortunately, it does not work straight out of the box in the setup described above. Using it as inspiration, though, I managed to figure out that I need a file called specs that I need to create in directory $HOME/gcc/gcc-4.9.0/lib/gcc/x86_64-unknown-freebsd9.2/4.9.0 by running there ../../../../bin/gcc -dumpspecs > specs Now, that file needs a correction, so open it with your farourite editor, find the line starting with *link:, and prepend this: %{!rpath: -rpath /home/ljw/gcc/gcc-4.9.0/lib/gcc/x86_64-unknown-freebsd9.2/4.9.0} to the line that follows. Now you may use the compiler to build some program, and when you try to see what shared libs it uses by typing ldd ./a.out, you will see libstdc++.so deep down from the compiler installation path.

Advertisements

2 Responses to Installation of gcc on a user account

  1. Notechus says:

    These are good hints you are giving here. I was wondering if you could write another one about installation of CLANG on Linux or Windows(or both ofc). Cheers

    • ljwo says:

      Thanks, but to be true I was just mostly guessing while I was doing that, and since these things are easily forgotten, I decided to write it up as much for myself as for everybody else. I never tried clang on Windows (I do keep OEM installations on machines that came with it but do not run it very often), and on debian jessie that powers my main machine clang is available as a package, and this is so much easier…

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: