Skip to content

GSL - GNU Scientific Library

GSL is an open-source numerical library, primarily for C and C++ use

The GSL library is available as a module on Apocrita. The library contains a wide variety of numerical routines including statistical, root finding, random numbers and sorting routines.

Using GSL

Compiling using GSL

GSL provides library objects and header files, along with a utility program to show the configuration of the installed library. On Apocrita GSL is provided both with and without an included CBLAS implementation.

To use a GSL routine in a C program it is necessary to use the appropriate header file when compiling and to link against the library. The header file will be named like gsl/gsl_category.h where category is the topic of the required functions. For example, to use the function gsl_sf_zeta_int to calculate the Riemann zeta function:

#include <stdio.h>
#include "gsl/gsl_sf_zeta.h"
int main (int argc, char *argv[]) {
  printf("zeta for n=2: %17.16f\n", gsl_sf_zeta_int(2));
  return 0;

The required header file is given in the documentation for the function.

GSL installations on Apocrita support using pkg-config and gsl-config to indicate the compile and link options to pass to the compiler when building code against GSL. For example, the output may look like:

$ module load gsl
$ gsl-config --cflags
$ gsl-config --libs
-L<gsl_path>/lib -lgsl -lgslcblas -lm
$ gsl-config --libs-without-cblas
-L<gsl_path>/lib -lgsl -lm

We recommend using gsl-config to ensure that all library dependencies and compilation flags are captured.

To put these values on the compilation command line one can use command substitution:

gcc my_gsl_using_program.c $(gsl-config --cflags --libs)

Using GSL with other programming languages

Although GSL is provided with a C programming interface compatible with C++, it is possible to use the library from other programming languages. Repositories for languages such as Python and R often include packages with interfaces for GSL and in Fortran the C API may be used either directly using the standard C interoperability features or indirectly using third-party libraries.

An interface for Fortran use is available on Apocrita, but interfacing libraries and packages are not currently installed system-wide on Apocrita for other languages.


We have installed the FGSL wrappers on Apocrita. The available modules can be seen with the command

module avail fgsl

The FGSL wrappers provide a Fortran module file which means that you must choose a module suitable for use with the compiler you are using. The modules are set up to depend on a particular compiler module; you will have a module load conflict if trying to use an incompatible compiler module.

The module help fgsl/<version> command shows the compatible compiler:

$ module help fgsl/1.3.0
Module Specific Help for ...:

   Adds fgsl (Intel) to your environment
   Version 1.3.0

With FGSL wrapping the GSL library, it is also necessary for the GSL library to be compatible. Again, the FGSL module is set up to automatically load a compatible GSL module and a conflict will arise if an incompatible GSL module was previously loaded. This also means that it is not necessary to load a GSL module in addition to an FGSL module to be able to use the wrappers and the underlying library.

The table below shows the currently installed module and its compatibility:

FGSL version Compatible GSL version Compatible compiler module
1.3.0 2.4 intel/2018.3

Once the FGSL, GSL and compiler modules have been loaded, the Fortran wrappers may be compiled using the options provided by pkg-config. For example, to compile with ifort:

ifort my_fgsl_prog.f90 `pkg-config --libs --cflags gsl`

The FGSL implementation provides a module fgsl which provides access to all definitions:

  use fgsl, only : fgsl_double
  real(fgsl_double) x
end program