Install GCC: Difference between revisions
| Line 60: | Line 60: | ||
cd ~/soft |
cd ~/soft |
||
mkdir gcc |
mkdir gcc-build |
||
cd gcc |
cd gcc-build |
||
Then we will 'configure' from that directory. Everything will be installed in ~/usr, including the executable compilers in ~/usr/bin and the library files in ~/usr/lib. To specify that you do |
Then we will 'configure' from that directory. Everything will be installed in ~/usr, including the executable compilers in ~/usr/bin and the library files in ~/usr/lib. To specify that you do |
||
cd ~/ |
cd ~/soft/gcc-build |
||
../gcc- |
../gcc-source/configure --prefix=$HOME/usr --with-local-prefix=$HOME/usr/local --with-gmp=$HOME/usr --with-mpfr=$HOME/usr --enable-languages=c++,fortran |
||
If you don't need the Fortran compiler specify --enable-languages=c++. (C compiler is enabled by default.) |
If you don't need the Fortran compiler specify --enable-languages=c++. (C compiler is enabled by default.) Even if you don't use Fortran it can be useful to compile Lapack for example. |
||
Other options could be specified at this point, for example, GCC 3 in wcr was configured with this options |
Other options could be specified at this point, for example, GCC 3 in wcr was configured with this options |
||
Revision as of 19:30, 14 December 2009
GCC stands for GNU Compiler Collection. The latest version is GCC 4.3. The use of this tutorial is to install GCC 4, which is not available in some linux distributions. In those systems GCC 3 is available instead but I suppose that for some reason you need GCC 4. (Of course we will use GCC 3, or any other available C compiler to build GCC 4.) The tutorial is focused in obtaining 'g++' (GNU C++) among all the available compilers in the collection. This tutorial is based on this other guide, although this version is much more straightforward.
You can check which version of GCC is currently installed by running
$gcc -v ... gcc version 3.4.6 20060404 (Red Hat 3.4.6-3)
This tutorial is mainly oriented to install in a campus cluster. If you want a bleeding edge compiler in your Linux computer it is better to just use an up to date distribution instead of going through these instructions. For example Ubuntu 9.10 has as default compiler gcc 4.4, just by doing:
sudo apt-get install gcc
Preparation and Downloads
The tutorial assumes that you want to install GCC/g++ in your userspace directories (i.e. in your home directory). To do that create the following directories:
mkdir ~/soft mkdir ~/usr
You will need the GMP library whose development (sources) are not installed in general (and in particular not in wcr).
cd ~/soft export GMP_VER=4.3.1 wget ftp://ftp.gmplib.org/pub/gmp-4.3.1/gmp-$GMP_VER.tar.gz tar -zxvf gmp-$GMP_VER.tar.gz cd gmp-$GMP_VER ./configure --prefix=$HOME/usr --with-local-prefix=$HOME/usr/local make make check make install
Which will create ~/usr/lib/libgmp.[a,la,so] and ~/usr/include/gmp.h. Also you will need the MPFR library after GMP.
cd ~/soft export MPFR_VER=2.4.2 wget http://www.mpfr.org/mpfr-current/mpfr-$MPFR_VER.tar.gz tar -zxvf mpfr-$MPFR_VER.tar.gz cd mpfr-$MPFR_VER ./configure --prefix=$HOME/usr --with-gmp=$HOME/usr make make check make install
This will create ~/usr/lib/libmpfr.[a,la,so] and ~/usr/include/mpfr.h.
Now, Download the sources to a local directory:
cd ~/soft mkdir gcc_source svn svn checkout svn://gcc.gnu.org/svn/gcc/trunk gcc_source
Compilation
This is important and different from other usual compilation procedures: GCC should be compiled in a directory different from the source directory, in this case we will create a "build" directory.
cd ~/soft mkdir gcc-build cd gcc-build
Then we will 'configure' from that directory. Everything will be installed in ~/usr, including the executable compilers in ~/usr/bin and the library files in ~/usr/lib. To specify that you do
cd ~/soft/gcc-build ../gcc-source/configure --prefix=$HOME/usr --with-local-prefix=$HOME/usr/local --with-gmp=$HOME/usr --with-mpfr=$HOME/usr --enable-languages=c++,fortran
If you don't need the Fortran compiler specify --enable-languages=c++. (C compiler is enabled by default.) Even if you don't use Fortran it can be useful to compile Lapack for example.
Other options could be specified at this point, for example, GCC 3 in wcr was configured with this options
... --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=x86_64-redhat-linux
Which may or may not be important for you. Check that the configure works (if it doesn't, report it in this wiki) before doing:
export LD_LIBRARY_PATH=$HOME/usr/lib export LD_RUN_PATH=$HOME/usr/lib make #takes ~60 minutes
Or you can take advantage of multiprocessor machines by requesting a parallel compilation:
export LD_LIBRARY_PATH=$HOME/usr/lib export LD_RUN_PATH=$HOME/usr/lib make -j 4 #takes only(?) ~30 minutes
where 4 can be replaced by the number of processors available.
You can then 'test' many of the bundled compilers by following the official test instructions. However I did not succeed in running the test due to the lack of certain packages in the system.
We do the final install:
make install
Important: there is no 'uninstall', if you want to remove GCC from ~/usr you have to do it manually which can be very difficult.
To test the version installed:
$ ~/usr/bin/g++ -v Using built-in specs. Target: x86_64-unknown-linux-gnu Configured with: ../gcc-4.3.3/configure --prefix=/home/correaa/usr --with-local-prefix=/home/correaa/usr/local --with-gmp=/home/correaa/usr --with-mpfr=/home/correaa/usr --enable-languages=c++ Thread model: posix gcc version 4.3.3 (GCC)
and do
~/usr/bin/g++ --print-search-dirs
to see which libraries will be used by default.
What is installed is difficult to describe, it includes binary executables and compiler wrapper installed in ~/usr/bin (for example g++), runtime and standard libraries in ~/usr/lib (and ~/usr/lib64) and a bunch of standard header files in ./include/c++/4.3.3. It also includes the Standard Template Library.
Basic Usage
This is the "Hello, world!" program
#include <iostream>
int main(){
std::cout << "Hello, world!\n";
}
which can be compiled with our brand new compiler:
cd /tmp wget http://micro.stanford.edu/mediawiki-1.11.0/images/Hello_world.cpp.tar -o hello_world.cpp.tar tar -xvf hello_word.cpp.tar ~/usr/bin/g++ -Wl,-rpath=$HOME/usr/lib:$HOME/usr/lib64 hello_world.cpp -o hello_world ./hello_world
Note that you have to specify the tilde (~) in order to run that specific compiler and not the default system one. The rpath option tells the compiler to use your local (home) version of the runtime and standard libraries instead of the default ones (in /usr/lib and /usr/lib64).
In any case you can check which libraries are being used by doing:
ldd ./hello_world
Now you are powerful and can tell your friends that you compiled a compiler. Now seriously, you can build programs that you could not compile or compiled with bugs with the system compiler.
Libraries
By following this tutorial you should have the C and C++ compilers of the GNU Compiler Collection, with them you can use any decent C or C++ library available, for example the Standard Template Library (included with GCC), FFTW, BOOST and HDF5.
Install MPICH2
This is the procedure to compile and install the MPICH2 library and environment. The instructions are taken from the official documentation and from this wiki-page.
mkdir ~/usr mkdir ~/soft cd ~/soft wget http://www.mcs.anl.gov/research/projects/mpich2/downloads/tarballs/1.0.8/mpich2-1.0.8.tar.gz tar -zxvf mpich2-1.0.8.tar.gz
Like GCC, MPICH2 should be compiled in a different directory than the location of the sources (this is not the case for version 1.1.:
mkdir mpich2-1.0.8-build cd mpich2-1.0.8-build ../mpich2-1.0.8/configure --prefix=$HOME/usr |& tee configure.log make |& tee make.log make install
The compilation takes ~20 minutes. (Do not try make -j 2 or make install only). The compiler wrappers (i.e. scripts to compile with MPI settings) are installed in ~/usr/bin, for example ~/usr/bin/mpicxx for the C++ MPI wrapper.
Now set up the environment, in your home directory create a configuration file. This need to be done only once.
cd ~/ touch .mpd.conf chmod 600 .mpd.conf echo 'MPD_SECRETWORD=nano' > .mpd.conf
Test example
Now you are ready to compile the example (based on this original example)
cd ~/tmp wget http://micro.stanford.edu/mediawiki-1.11.0/images/Mpich2_example.tar -o mpich2_example.tar tar -xvf mpich2_example.tar cd mpich2_example ~/usr/bin/mpicxx mpich2_example.cpp -o mpich2_example
Before executing a MPI program you will need to start the MPI daemon
~/usr/bin/mpd &
And then the program can be run
~/usr/bin/mpirun -np 2 ./mpich2_example