6

I got a problem while installing the GNU Scientific Library (gsl). I put the package on my desktop, and did "./configure", "make", and "sudo make install", according to the document included. I checked the /usr/local/include directory, there is a newly created "gsl" folder in there. But When I tried to use the functions provided by the library, the "undefined reference to 'gsl_sf_beta_inc'" error occurred. Here is my code.

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

int main (void)
{
    double a = 20;
    double b = 1000;
    double x = 0.5;
    double result = gsl_sf_beta_inc(a, b, x);
    printf("%f/d", result);
    return 0;
}

I sensed that the problem might be caused by the fact I put the package on the desktop, so the binary code generated by the "make" command goes there, which is wrong. So, is my guess correct? If it is, where should I put them? If it is not, what should I do? Thanks.

seemuch
  • 583
  • 3
  • 7
  • 18

2 Answers2

12

You need to link the library, assuming the make install was successful.

The gsl's documentation says this should work
(note the two necessary linking options for gsl to work: "-lgsl -lgslcblas"):

gcc -I/usr/local/include -L/usr/local/lib main.c -o main -lgsl -lgslcblas -lm

Alternative "cblas" instead of gsl's cblas is also possible as per: alternate cblas for gsl

Loves Probability
  • 819
  • 1
  • 8
  • 15
Vinicius Kamakura
  • 7,299
  • 1
  • 25
  • 42
  • Thank you Dude, but it does not work. Actually, if I run the command you provided, everything will be "undefined reference"-d. But I think you pointed to a right direction. I am new to programming, so I am not familiar with this "linking" thing. I will work on that. Any further comment is welcome. – seemuch Aug 11 '11 at 23:19
  • But really, if install was successful, you simply need to tweak the line I provided with the real paths (which I think are correct, since you said there is a gsl folder in /usr/src/include). – Vinicius Kamakura Aug 11 '11 at 23:24
  • I tried this one: gcc -I/usr/local/include -L/usr/local/lib main.c -o main -lgsl -l gslcblas, and it worked. Thank you! – seemuch Aug 11 '11 at 23:29
  • You are welcome dude, just don't forget to accept the answer if you find it worth it. – Vinicius Kamakura Aug 12 '11 at 00:02
3

Use pkg-config --libs gsl to find out what the necessary linkers are to be and then just link them. An optional thing would be to check pkg-config --cflags gsl. The second gives you the directory of the include files if they are not installed in the default /usr/include/ directory. If you've installed it there you can just ignore that.
The output of pkg-config --libs gsl would be
-lgsl -lgslcblas -lm
That means that those three have to be linked. So while compiling your program you do that by,
gcc name.c -lgsl -lgslcblas -lm

  • More simply, you can just write `gcc -o name name.c $(pkg-config --cflags --libs gsl)`, or (in a reasonable Makefile) `LDFLAGS+=$(shell pkg-config --libs gsl)` and `CFLAGS+=$(shell pkg-config --cflags gsl)`. – Toby Speight Nov 22 '16 at 18:14