PDA

View Full Version : Undefined Reference for GSL library calls


Juanito
October 3rd, 2006, 11:22 PM
Hey guys,

I'm in a bit of a time-bind, so I thought I'd ask here after spending 2 hours scouring the internet for an answer.

I am using the GNU Science Library

http://www.gnu.org/software/gsl/manual/html_node/Compiling-and-Linking.html#Compiling-and-Linking

After including the proper header files
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>


After pressing Build in KDevelop or Anjuta, I'm getting errors like...

undefined reference to `gsl_ran_multinomial'

From what I've read, it's something about linking? I'm used to good ol' java and visual studio, so I have no clue what to do.

Thanks for your patience!

hod139
October 4th, 2006, 09:43 PM
You are correct, it is a linking issue. I'm not sure how to specify external libraries in Kdevelop or Anjunta, but on the command line you would do this:

gcc myfile.c -lgsl

the "-l" flag tells gcc to link against the gsl library. You will need to tell an IDE to also link against gsl.

You most likely will also want -lm (the math lib).

jwilley44
June 5th, 2008, 08:12 PM
Hello, I had the same problem. I tried compiling with -lgsl and I got a bunch of errors. The errors were of the form:

/usr/lib/gcc/x86 64-linux-gnu/4.2.3/../../../../lib/libgsl.so undefined refernce to `cblas_csyrk'

with the last part between the ` and the ' varying.

Thanks in advance

WW
June 5th, 2008, 09:57 PM
The best way to get the correct arguments to link the GSL library is with the pkg-config command:

$ pkg-config --libs gsl

That will print the options required to link the GSL library. You can also use the --cflags option to get the correct compiler options:

$ pkg-config --cflags gsl

(That may just print a blank line, meaning no special options are required, since the header files are in a standard directory.)

To compile and link a program that is contained in a single C file that uses GSL:

$ gcc myprogram.c `pkg-config --cflags --libs gsl` -o myprogram

Or, to compile and then link,

$ gcc -c myprogram.c `pkg-config --cflags gsl`
$ gcc myprogram.o `pkg-config --libs gsl` -o myprogram


I don't use KDevelop or Anjuta, but I am sure there is a standard way to tell them to use the required compiler options.

abhinandkr
June 17th, 2009, 06:23 AM
Yes it works!
And, you can simply create a shell file to execute that compile and link statement. Give the o/p file as a.out if required.