Install GCC: Difference between revisions
No edit summary |
|||
| (86 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
(written by Alfredo Correa) |
|||
GCC stands for GNU Compiler Collection. The latest version is GCC 4.3. |
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. |
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. |
||
| Line 11: | Line 13: | ||
gcc version 3.4.6 20060404 (Red Hat 3.4.6-3) |
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: |
|||
= Preparation = |
|||
sudo aptitude install gcc g++ |
|||
''If you are lucky, and not interested in the details you can donwload this [[Media:gcc_install.sh.txt|script]] and run it in your computer to install GCC from scratch. It downloads, compiles and installs to your ~/usr directory''. (It takes ~30 minutes in a dual core plus the download time.) |
|||
= 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: |
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 |
|||
== MP libraries == |
|||
You will need the [http://gmplib.org/ GMP library] whose development (sources) are not installed in general (and in particular not in wcr). |
|||
GCC depends on several libraries and tools most of which are usually in the system already, except for [http://gmplib.org/ GMP library], [http://www.mpfr.org/ MPFR] and [http://www.multiprecision.org/ MPC]. |
|||
cd ~/soft |
|||
Fortunately we don't need to install these three libraries explicitly. The GCC installation takes care of their compilation as long as the sources directories are put in the GCC source directory. The three directories should be named gmp, mpfr and mpc. |
|||
wget ftp://ftp.gnu.org/gnu/gmp/gmp-4.2.4.tar.gz |
|||
tar -zxvf gmp-4.2.4.tar.gz |
|||
cd gmp-4.2.4 |
|||
./configure --prefix=$HOME/usr --with-local-prefix=$HOME/usr/local |
|||
make |
|||
make check |
|||
make install |
|||
So, first download the GCC sources. |
|||
Which will create ~/usr/lib/libgmp.[a,la,so] and ~/usr/include/gmp.h. |
|||
Also you will need the [http://www.mpfr.org/ MPFR] library after GMP. |
|||
cd ~/soft |
|||
export GCC_VER=4.5.0 |
|||
wget http://www.mpfr.org/mpfr-current/mpfr-2.4.1.tar.gz |
|||
tar -zxvf mpfr-2.4.1.tar.gz |
|||
cd mpfr-2.4.1 |
|||
./configure --prefix=$HOME/usr --with-gmp=$HOME/usr |
|||
make |
|||
make check |
|||
make install |
|||
* from internet: |
|||
This will create ~/usr/lib/libmpfr.[a,la,so] and ~/usr/include/mpfr.h. |
|||
wget http://gcc-uk.internet.bs/releases/gcc-$GCC_VER/gcc-$GCC_VER.tar.gz |
|||
Now, Download the sources to a local directory: |
|||
tar -zxvf gcc-$GCC_VER.tar.gz |
|||
mv gcc-$GCC_VER gcc-$GCC_VER-source |
|||
Check for last release at http://gcc-uk.internet.bs/releases/ |
|||
* or from SVN: |
|||
cd ~/soft |
|||
svn checkout svn://gcc.gnu.org/svn/gcc/trunk |
|||
tar -zxvf gcc-g++-4.3.3.tar.gz |
|||
and then download and uncompress GMP, MPFR and MPC |
|||
= Compilation = |
|||
cd ~/soft |
|||
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. |
|||
export GMP_VER=5.0.2 |
|||
wget ftp://ftp.gmplib.org/pub/gmp-$GMP_VER/gmp-$GMP_VER.tar.gz |
|||
tar -zxvf gmp-$GMP_VER.tar.gz |
|||
mv gmp-$GMP_VER gcc-$GCC_VER-source/gmp |
|||
export MPFR_VER=3.0.1 |
|||
wget http://www.mpfr.org/mpfr-current/mpfr-$MPFR_VER.tar.gz |
|||
tar -zxvf mpfr-$MPFR_VER.tar.gz |
|||
mv mpfr-$MPFR_VER gcc-$GCC_VER-source/mpfr |
|||
export MPC_VER=0.8.1 |
|||
wget http://www.multiprecision.org/mpc/download/mpc-$MPC_VER.tar.gz |
|||
tar -zxvf mpc-$MPC_VER.tar.gz |
|||
mv mpc-$MPC_VER gcc-$GCC_VER-source/mpc |
|||
The source directory is ready to for compilation. |
|||
cd ~/soft |
|||
mkdir gcc-4.3.3-build |
|||
cd gcc-4.3.3-build |
|||
== GNU Binutils == |
|||
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 |
|||
In some systems binutils is so old (for example, v2.17) that it won't work with the new compiler. In this case you need to install Binutils in order to succeed in the compilation of a program |
|||
cd ~/usr/gcc-4.3.3-build |
|||
../gcc-4.3.3/configure --prefix=$HOME/usr --with-local-prefix=$HOME/usr/local --with-gmp=$HOME/usr --with-mpfr=$HOME/usr --enable-languages=c++,fortran |
|||
cd ~/soft |
|||
If you don't need the Fortran compiler specify --enable-languages=c++. (C compiler is enabled by default.) |
|||
wget http://ftp.gnu.org/gnu/binutils/binutils-2.20.tar.gz |
|||
tar -zxvf binutils-2.20.tar.gz |
|||
cd binutils-* |
|||
./configure --prefix=$HOME/usr |
|||
make |
|||
make install |
|||
This will install the linker (ld), the library archive (ar) and other programs in ~/usr/bin. Later they will be used by the compiler, for example, by ~/usr/bin/c++. |
|||
Other options could be specified at this point, for example, GCC 3 in wcr was configured with this options |
|||
= Compilation = |
|||
... --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 |
|||
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. |
|||
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: |
|||
cd ~/soft |
|||
export LD_LIBRARY_PATH=$HOME/usr/lib |
|||
mkdir gcc-$GCC_VER-build |
|||
export LD_RUN_PATH=$HOME/usr/lib |
|||
cd gcc-$GCC_VER-build |
|||
make #takes ~60 minutes |
|||
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 |
|||
Or you can take advantage of multiprocessor machines by requesting a parallel compilation: |
|||
cd ~/soft/gcc-$GCC_VER-build |
|||
export LD_LIBRARY_PATH=$HOME/usr/lib |
|||
../gcc-$GCC_VER-source/configure --prefix=$HOME/usr --bindir=$HOME/usr/bin$CLUSTER --prefix=$HOME/usr/lib$CLUSTER --enable-languages=c++,fortran --enable-shared |
|||
export LD_RUN_PATH=$HOME/usr/lib |
|||
make -j 4 #takes only(?) ~30 minutes |
|||
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. |
|||
where 4 can be replaced by the number of processors available. |
|||
Check that the configure works (if it doesn't, report it in this wiki) before doing: |
|||
You can then 'test' many of the bundled compilers by following the [http://gcc.gnu.org/install/test.html official test instructions]. However I did not succeed in running the test due to the lack of certain packages in the system. |
|||
make -j 8 #takes ~60 minutes |
|||
We do the final install: |
|||
and finally: |
|||
make 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: |
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/usr --with-local-prefix=$HOME/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 |
and do |
||
~/usr/bin/g++ --print-search-dirs |
|||
to see which libraries will be used by default. |
to see which libraries and directories will be used by default. |
||
The installation also includes the C++ Standard Template Library. |
|||
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 = |
= Basic Usage = |
||
| Line 129: | Line 143: | ||
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. |
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. |
||
If you get an error that looks like |
|||
/tmp/ccw54L0X.s: Assembler messages: |
|||
/tmp/ccw54L0X.s:9: Error: unknown pseudo-op: `.cfi_personality' |
|||
the install [GNU Binutils], as described above. |
|||
= Check GCC version during compilation = |
|||
When we install our own version of GCC is because we need a certain feature of the new version that is not available in the old/system version. In general that means that the code using new features will not compile with the old compiler, and sometimes it will give some undecipherable syntax error. Because of that you may want to check that portions of your code actually do not compile with a certain old version of GCC, the macro language can verify conditions on the GCC version. For example you can add the following code at the beginning of a code file or inside a function that uses certain feature |
|||
void very_modern_function(){ |
|||
#if not((__GNUC__>=4) and (__GNUC_MINOR__ >1)) |
|||
#error "expected gcc version > 4.1, got " ## __GNUC__ ## "." ## __GNUC_MINOR__ |
|||
#endif |
|||
... code with new feautures ... |
|||
} |
|||
If you use a compiler with version 4.1 or earlier, the output will show |
|||
./example.cpp:23: error: #error "expected gcc version > 4.1" |
|||
before any other error message related to features used within the function. (You can make the behaviour less severe by replacing "#error" for "#warning" so that compilation can eventually succeed.) |
|||
Alternatively (if you use Boost) you can have it in one line |
|||
BOOST_STATIC_ASSERT((__GNUC__>=4) and (__GNUC_MINOR__ >1)); |
|||
with the error message |
|||
./example.cpp:22: error: invalid application of ‘sizeof’ to incomplete type ‘boost::STATIC_ASSERTION_FAILURE<false>’ |
|||
This trick avoid the use the wrong compiler version. |
|||
= Libraries = |
= 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), [[Install FFTW3|FFTW]], [[BOOST Library|BOOST]] and [[Install HDF5|HDF5]]. |
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), [[Install FFTW3|FFTW]], [[BOOST Library|BOOST]] and [[Install HDF5|HDF5]]. |
||
== Install MPICH2 == |
|||
This is the procedure to compile and install the [http://www.mcs.anl.gov/research/projects/mpich2/ MPICH2 library] and environment. The instructions are taken from [http://www.mcs.anl.gov/research/projects/mpich2/documentation/files/mpich2-1.0.8-installguide.pdf the official documentation] and from [http://source.ggy.bris.ac.uk/wiki/Install_and_configure_MPI#Download_MPI this wiki-page]. |
|||
mkdir ~/usr |
|||
mkdir ~/soft |
|||
cd ~/soft |
|||
export MPICH2_VER=1.2.1 |
|||
wget http://www.mcs.anl.gov/research/projects/mpich2/downloads/tarballs/$MPICH2_VER/mpich2-$MPICH2_VER.tar.gz |
|||
tar -zxvf mpich2-$MPICH2_VER.tar.gz |
|||
Like GCC, MPICH2 should be compiled in a different directory than the location of the sources: |
|||
mkdir mpich2-$MPICH2_VER-build |
|||
cd mpich2-$MPICH2_VER-build |
|||
../mpich2-$MPICH2_VER/configure --prefix=$HOME/usr |& tee configure.log |
|||
time make |& tee make.log |
|||
make install |
|||
The compilation takes ~20 minutes. (Do not try <code>make -j 2</code> or <code>make install</code> 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 [[Media:mpich2_example.tar|example]] (based on this [http://www.mcs.anl.gov/research/projects/mpi/usingmpi/examples/simplempi/main.htm 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 |
|||
== Install GSL == |
|||
For quick installation [http://www.gnu.org/software/gsl/ the GSL library] in Ubuntu try <code>sudo aptitude install libgsl0-dev</code> which install 1.13 in U10.04. To install from scratch: |
|||
mkdir ~/soft |
|||
cd ~/soft |
|||
wget http://mirrors.kernel.org/gnu/gsl/gsl-1.14.tar.gz |
|||
tar -zxvf gsl-1.14.tar.gz |
|||
cd gsl-1.14 |
|||
./configure --prefix=$HOME/usr |
|||
make --jobs=2 |
|||
make check > log 2>&1 |
|||
make install |
|||
It takes 2 minutes to compile and one minute to run a few tests. |
|||
The header files will be located in <code>~/usr/include/gsl/*.h</code> and the libraries (static and dynamic) will be located at <code>~/usr/libgsl*.{a,la,so}</code> |
|||
A program using GSL must be linked to libgsl and other libraries (gslblas and lm): |
|||
gcc program.c -L$HOME/usr/lib -lgsl -lgslcblas -lm |
|||
This line can be generated automatically by pkgconfig |
|||
gcc program.c `pkgconfig --libs gsl` |
|||
if we have set (e.g. in bashrc) the variable: |
|||
export PKG_CONFIG_PATH=$HOME/usr/lib/pkgconfig |
|||
== Install GMP, MPFR and GMPFRXX == |
|||
GMP is necessary for the compilation of the C++ compiler, in the instructions above it is installed automatically. It can also be installed for general use: |
|||
# see above for download |
|||
cd ~/sotf/gmp-$GMP_VER |
|||
./configure --prefix=$HOME/usr --bindir=$HOME/usr/bin$CLUSTER --libdir=$HOME/usr/lib$CLUSTER |
|||
time make |
|||
make install |
|||
(1 minute) |
|||
To uninstall |
|||
rm -f ~/usr/include/gmp.h ~/usr/include/gmpxx.h ~/usr/lib/libgmp*.* ~/usr/info/gmp* |
|||
MPFR has a similar installation |
|||
# see above for download |
|||
cd ~/soft/mpfr-$MPFR_VER |
|||
./configure --prefix=$HOME/usr --libdir=$HOME/usr/lib$CLUSTER |
|||
make install |
|||
In addition, the a GMP/MPFR C++ wrapper with automatic rounding (used by Boost.Math bindings, http://www.boost.org/doc/libs/1_43_0/libs/math/doc/sf_and_dist/html/math_toolkit/using_udt/use_mpfr.html) can be installed (more manual) |
|||
cd ~/soft |
|||
wget http://math.berkeley.edu/~wilken/code/gmpfrxx/gmpfrxx.zip |
|||
unzip gmpfrxx.zip |
|||
cd gmpfrxx/ |
|||
# add lines #include<cstdio> \\ #include<cstdlib> |
|||
mv gmpfrxx.h tmp.h |
|||
echo '#include<cstdio>' > gmpfrxx.h |
|||
echo '#include<cstdlib>' >> gmpfrxx.h |
|||
cat tmp.h >> gmpfrxx.h |
|||
make libgmpfrxx.a |
|||
cp gmpfrxx.h ~/usr/include |
|||
cp libgmpfrxx.a ~/usr/lib$CLUSTER |
|||
Latest revision as of 20:12, 14 October 2011
(written by Alfredo Correa)
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 aptitude install gcc g++
If you are lucky, and not interested in the details you can donwload this script and run it in your computer to install GCC from scratch. It downloads, compiles and installs to your ~/usr directory. (It takes ~30 minutes in a dual core plus the download time.)
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
MP libraries
GCC depends on several libraries and tools most of which are usually in the system already, except for GMP library, MPFR and MPC. Fortunately we don't need to install these three libraries explicitly. The GCC installation takes care of their compilation as long as the sources directories are put in the GCC source directory. The three directories should be named gmp, mpfr and mpc.
So, first download the GCC sources.
cd ~/soft export GCC_VER=4.5.0
- from internet:
wget http://gcc-uk.internet.bs/releases/gcc-$GCC_VER/gcc-$GCC_VER.tar.gz tar -zxvf gcc-$GCC_VER.tar.gz mv gcc-$GCC_VER gcc-$GCC_VER-source
Check for last release at http://gcc-uk.internet.bs/releases/
- or from SVN:
svn checkout svn://gcc.gnu.org/svn/gcc/trunk
and then download and uncompress GMP, MPFR and MPC
cd ~/soft export GMP_VER=5.0.2 wget ftp://ftp.gmplib.org/pub/gmp-$GMP_VER/gmp-$GMP_VER.tar.gz tar -zxvf gmp-$GMP_VER.tar.gz mv gmp-$GMP_VER gcc-$GCC_VER-source/gmp export MPFR_VER=3.0.1 wget http://www.mpfr.org/mpfr-current/mpfr-$MPFR_VER.tar.gz tar -zxvf mpfr-$MPFR_VER.tar.gz mv mpfr-$MPFR_VER gcc-$GCC_VER-source/mpfr export MPC_VER=0.8.1 wget http://www.multiprecision.org/mpc/download/mpc-$MPC_VER.tar.gz tar -zxvf mpc-$MPC_VER.tar.gz mv mpc-$MPC_VER gcc-$GCC_VER-source/mpc
The source directory is ready to for compilation.
GNU Binutils
In some systems binutils is so old (for example, v2.17) that it won't work with the new compiler. In this case you need to install Binutils in order to succeed in the compilation of a program
cd ~/soft wget http://ftp.gnu.org/gnu/binutils/binutils-2.20.tar.gz tar -zxvf binutils-2.20.tar.gz cd binutils-* ./configure --prefix=$HOME/usr make make install
This will install the linker (ld), the library archive (ar) and other programs in ~/usr/bin. Later they will be used by the compiler, for example, by ~/usr/bin/c++.
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-$GCC_VER-build cd gcc-$GCC_VER-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-$GCC_VER-build ../gcc-$GCC_VER-source/configure --prefix=$HOME/usr --bindir=$HOME/usr/bin$CLUSTER --prefix=$HOME/usr/lib$CLUSTER --enable-languages=c++,fortran --enable-shared
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.
Check that the configure works (if it doesn't, report it in this wiki) before doing:
make -j 8 #takes ~60 minutes
and finally:
make install
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/usr --with-local-prefix=$HOME/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 and directories will be used by default.
The installation also includes the C++ 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.
If you get an error that looks like
/tmp/ccw54L0X.s: Assembler messages: /tmp/ccw54L0X.s:9: Error: unknown pseudo-op: `.cfi_personality'
the install [GNU Binutils], as described above.
Check GCC version during compilation
When we install our own version of GCC is because we need a certain feature of the new version that is not available in the old/system version. In general that means that the code using new features will not compile with the old compiler, and sometimes it will give some undecipherable syntax error. Because of that you may want to check that portions of your code actually do not compile with a certain old version of GCC, the macro language can verify conditions on the GCC version. For example you can add the following code at the beginning of a code file or inside a function that uses certain feature
void very_modern_function(){
#if not((__GNUC__>=4) and (__GNUC_MINOR__ >1))
#error "expected gcc version > 4.1, got " ## __GNUC__ ## "." ## __GNUC_MINOR__
#endif
... code with new feautures ...
}
If you use a compiler with version 4.1 or earlier, the output will show
./example.cpp:23: error: #error "expected gcc version > 4.1"
before any other error message related to features used within the function. (You can make the behaviour less severe by replacing "#error" for "#warning" so that compilation can eventually succeed.)
Alternatively (if you use Boost) you can have it in one line
BOOST_STATIC_ASSERT((__GNUC__>=4) and (__GNUC_MINOR__ >1));
with the error message
./example.cpp:22: error: invalid application of ‘sizeof’ to incomplete type ‘boost::STATIC_ASSERTION_FAILURE<false>’
This trick avoid the use the wrong compiler version.
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 export MPICH2_VER=1.2.1 wget http://www.mcs.anl.gov/research/projects/mpich2/downloads/tarballs/$MPICH2_VER/mpich2-$MPICH2_VER.tar.gz tar -zxvf mpich2-$MPICH2_VER.tar.gz
Like GCC, MPICH2 should be compiled in a different directory than the location of the sources:
mkdir mpich2-$MPICH2_VER-build cd mpich2-$MPICH2_VER-build ../mpich2-$MPICH2_VER/configure --prefix=$HOME/usr |& tee configure.log time 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
Install GSL
For quick installation the GSL library in Ubuntu try sudo aptitude install libgsl0-dev which install 1.13 in U10.04. To install from scratch:
mkdir ~/soft cd ~/soft wget http://mirrors.kernel.org/gnu/gsl/gsl-1.14.tar.gz tar -zxvf gsl-1.14.tar.gz cd gsl-1.14 ./configure --prefix=$HOME/usr make --jobs=2 make check > log 2>&1 make install
It takes 2 minutes to compile and one minute to run a few tests.
The header files will be located in ~/usr/include/gsl/*.h and the libraries (static and dynamic) will be located at ~/usr/libgsl*.{a,la,so}
A program using GSL must be linked to libgsl and other libraries (gslblas and lm):
gcc program.c -L$HOME/usr/lib -lgsl -lgslcblas -lm
This line can be generated automatically by pkgconfig
gcc program.c `pkgconfig --libs gsl`
if we have set (e.g. in bashrc) the variable:
export PKG_CONFIG_PATH=$HOME/usr/lib/pkgconfig
Install GMP, MPFR and GMPFRXX
GMP is necessary for the compilation of the C++ compiler, in the instructions above it is installed automatically. It can also be installed for general use:
# see above for download cd ~/sotf/gmp-$GMP_VER ./configure --prefix=$HOME/usr --bindir=$HOME/usr/bin$CLUSTER --libdir=$HOME/usr/lib$CLUSTER time make make install
(1 minute)
To uninstall
rm -f ~/usr/include/gmp.h ~/usr/include/gmpxx.h ~/usr/lib/libgmp*.* ~/usr/info/gmp*
MPFR has a similar installation
# see above for download cd ~/soft/mpfr-$MPFR_VER ./configure --prefix=$HOME/usr --libdir=$HOME/usr/lib$CLUSTER make install
In addition, the a GMP/MPFR C++ wrapper with automatic rounding (used by Boost.Math bindings, http://www.boost.org/doc/libs/1_43_0/libs/math/doc/sf_and_dist/html/math_toolkit/using_udt/use_mpfr.html) can be installed (more manual)
cd ~/soft wget http://math.berkeley.edu/~wilken/code/gmpfrxx/gmpfrxx.zip unzip gmpfrxx.zip cd gmpfrxx/ # add lines #include<cstdio> \\ #include<cstdlib> mv gmpfrxx.h tmp.h echo '#include<cstdio>' > gmpfrxx.h echo '#include<cstdlib>' >> gmpfrxx.h cat tmp.h >> gmpfrxx.h make libgmpfrxx.a cp gmpfrxx.h ~/usr/include cp libgmpfrxx.a ~/usr/lib$CLUSTER