A1. A Brief Tour of Some Other Useful C Libraries

Table of Contents


Since C is such a "small" language, you must typically rely on libraries and APIs for complex tasks (unless you code them up yourself). Here is a short list of some common libraries that you might find useful.



The GLib library contains many useful data structures and building blocks such as linked lists, hash tables, and others.

Here is a link to a guide taking you through how to use GLib to manage data structures of the sort that you get "for free" in languages like Matlab, Python, etc… things like lists, hash tables (otherwise known as dictionaries), arrays, trees, etc. The guide also shows how to link to GLib at compile time.

Manage C data using the GLib collections

GNU Scientific Library (GSL)


The GNU Scientific Library provides a broad range of numerical and mathematical functions. If you want to do something in C that you have been doing in MATLAB, or Python/NumPy/SciPy, or R, then probably you will need to look at something like GSL for functions you need (or code them up yourself).

There is a list of main categories here. Things you might be interested are:

Linking to gsl

Of course before you can link to the gsl library, you have to make sure it is installed on your computer. See the gsl documentation for how to install it.

To link your code to the gsl you will have to do two things. First you will have to use the #include directive to include the relevant header files. In the gsl documentation for each function, it tells you which header file(s) you will need. Second, you will have to use a flag when compiling to tell the compiler to include the library files themselves. Here is an example program from the gsl documentation that uses linear algebra routines to solve a linear system \(Ax=b\):

[ 0.18 0.60 0.57 0.96 ] [x0]   [1.0]
[ 0.41 0.24 0.99 0.58 ] [x1] = [2.0]
[ 0.14 0.30 0.97 0.66 ] [x2]   [3.0]
[ 0.51 0.13 0.19 0.85 ] [x3]   [4.0]
// gcc -o go go.c -lgsl -lgslcblas

#include <stdio.h>
#include <gsl/gsl_linalg.h>

int main (void)
  double a_data[] = { 0.18, 0.60, 0.57, 0.96,
          0.41, 0.24, 0.99, 0.58,
          0.14, 0.30, 0.97, 0.66,
          0.51, 0.13, 0.19, 0.85 };

  double b_data[] = { 1.0, 2.0, 3.0, 4.0 };

  gsl_matrix_view m 
    = gsl_matrix_view_array (a_data, 4, 4);

  gsl_vector_view b
    = gsl_vector_view_array (b_data, 4);

  gsl_vector *x = gsl_vector_alloc (4);

  int s;

  gsl_permutation * p = gsl_permutation_alloc (4);

  gsl_linalg_LU_decomp (&m.matrix, p, &s);

  gsl_linalg_LU_solve (&m.matrix, p, &b.vector, x);

  printf ("x = \n");
  gsl_vector_fprintf (stdout, x, "%g");

  gsl_permutation_free (p);
  gsl_vector_free (x);
  return 0;
plg@wildebeest:~$ gcc -o go go.c -lgsl -lgslcblas
plg@wildebeest:~$ ./go
x = 


The blas and lapack libraries are high-performance libraries for linear algebra. If you are dealing with large matrices and you have the need for speed, then probably you should look into using BLAS / LAPACK, it will probably be faster than GSL.


The Apophenia library is an open source library for working with data sets, statistics, and statistical models.

The Apophenia library provides functions for dealing with data (reading it in, storing it in convenient data structures, and writing it to files). It also provides functions for dealing with data using the SQL database language. This allows you to perform searches on data, extract subsets of data, etc.

Apophenia has lots of functions to fit data to statistical models such as OLS models (ordinary least squares), it provides for statistical tests like t-tests, F-tests, and it includes models like logit, probit, and multinomial models. It also includes maximum likelihood methods, Bayesian updating, and resampling methods like bootstrapping.

There is extensive online documentation and example code on the Apophenia website here. I suggest you dive deeper on your own if you want to explore it in more detail.

Modeling with Data (book)

Apophenia is maintained by Ben Klemens, who also wrote a book Modeling With Data that makes use of Apophenia. You can buy the book or you can download it for free, the author makes it available on the website here.

I highly recommend the book not only as a guide to using the Apophenia library, but more generally as a guide to how to use C on a daily basis for data analysis and modeling. Ben argues that really, C is not that much more of a pain in the neck than "higher-level" languages like Python and R, but C is way faster (in terms of execution speed). It's worth a read.



GNUplot is not an external C library but in fact is a standalone program for making graphs and figures. I include it here because it is a popular UNIX tool that you will likely see in your C travels. The Apophenia library and the accompanying book Modeling With Data include lots of examples of using GNUplot to graphically visualize data, and fitted statistical models.

There is also good online documentation, tutorials and help and demos of GNUplot functionality. Anything you can do with Matlab, Python/Matplotlib, R, etc you can likely also do in GNUplot.

Paul Gribble | Summer 2012
This work is licensed under a Creative Commons Attribution 4.0 International License
Creative Commons License