Using the GNU Scientific Library on a Mac

This morning I installed, compiled, and ran a simple example program using the GNU Scientific Library. This took me a while to figure out, so I’ll share the process here. I am assuming that the reader, like the author, has only vague familiarity with C.

  • Install Homebrew, their site has a line of code you can run from the command line.
  • Install Xcode from the Apple Applications menu.
  • Install GSL, the GNU Scientific Library, using Homebrew. This will install GSL at /usr/local/include.
brew install gsl
  • Hammer out a sample program on your favorite editor (Vim) and name it main.c or something.
#include <stdio.h>
#include <gsl/gsl_sf_bessel.h>

int main( int argc, const char *argv[] ) {
    double x = 5.0 ;
    double y = gsl_sf_bessel_J0( x ) ;
    printf("J0(%g) = % .18e\n", x, y ) ;
    return 0 ;
}
  • Compile the code. This should produce an object file in your working directory called main.o.
gcc -Wall -I/usr/local/inlcude main.c
  • Link the object file to produce an executable, a.out. Here, the -L flag provides the path to the library, and the -l flag provides the name of the library that you’d like to link.
gcc -L/user/local/include -lgsl main.o 
  • Run the executable a.out from the command line.
./a.out

This should produce the following output:

J0(5) = -1.775967713143382642e-01

2 thoughts on “Using the GNU Scientific Library on a Mac”

  1. Great help!
    I found a couple typos though.
    – On compile: “inlcude”, should be “include”
    – On Linking: “/include” needs to be replaced with /lib

Comments are closed.