Install GSL

From Micro and Nano Mechanics Group
Jump to navigation Jump to search

The installation of the GNU Scientific Library is very simple and it worth using it for solving many simple numerical problems. The manual is very complete and easy to understand. It can also be useful to learn about the methods.

Installation

mkdir ~/soft
cd ~/soft

Direct Download

VERSION=1.15
wget ftp://ftp.gnu.org/gnu/gsl/gsl-$VERSION.tar.gz
tar -zxvf gsl-$VERSION.tar.gz
cd gsl-$VERSION

Version Control

(no working)

GSL development team uses Bazaar as version control system. First check that you have Autotools installed

sudo apt-get install autoconf automake libtool

then

mkdir -p ~/soft/gsl.bzr
cd ~/soft/gsl.bzr
bzr branch http://bzr.savannah.gnu.org/r/gsl/trunk
cd trunk
./autogen.sh
grep GSL_VERSION gsl_version.h

Compile

./configure --prefix=$HOME/usr CFLAGS="-fexceptions"
time make --jobs 2 install

to remove:

rm -rf ~/usr/include/gsl
rm -rf ~/usr/lib/libgsl*

C++ Exceptions

The only subtle point on the compilation step is that we need to activate "-fexceptions" if we want to use C++ exceptions with GSL. GSL is written in and for plain C, without C++ exceptions in mind. Although it can be used from C++ natively. By default, most error reports are done by returning an status value or function pointer error handlers and not by exceptions. If -fexceptions flag not active during compilation any error function called from GSL that throws exceptions won't be catched in a try/catch block.

Once -fexceptions is active in GSL binaries, C++ code can convert usual abort/errors into nice C++ exceptions:

#include<gsl/gsl_errno.h>
#include<string>
#include<boost/lexical_cast.hpp>
#include<stdexcept>
#include<cassert>
namespace gsl{namespace error{
 struct exception : std::runtime_error{
  int const code_;
  exception(std::string const& c, int gsl_errno) : std::runtime_error(c), code_(gsl_errno){}
   int code() const{return code_;}
 };
 void handler(const char * reason, const char * file, int line, int gsl_errno){
   throw exception(
     std::string(reason)
     + " at " + std::string(file) + ":"+boost::lexical_cast<std::string>(line) 
     + " error code "+boost::lexical_cast<std::string>(gsl_errno) + ": " 
     + std::string(gsl_strerror(gsl_errno)),
     gsl_errno
   );
 }
 gsl_error_handler_t* set_handler(void (*a)(const char*, const char*, int, int)){
   return gsl_set_error_handler(a);
 }
 static gsl_error_handler_t* const native = set_handler(&handler);
}}

The rest of the code can be unchanged, the difference being that now abort errors can be handled by catching.