Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ unable to link libgsl

Tags:

c++

linker

gsl

I try to use the gsl library in one project, but I can't get the example program from the gsl website run properly. The source code and all commands are taken from the website: https://www.gnu.org/software/gsl/manual/html_node/Using-the-library.html#Using-the-library

The program is the following (test.cpp):

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

int main (void) {
  double x = 5.0;
  double y = gsl_sf_bessel_J0 (x);
  printf ("J0(%g) = %.18e\n", x, y); 
  return 0;
}

Then I first compile without errors. But the linking fails:

$ g++ -Wall -I/usr/include/ -c test.cpp
$ g++ -L/usr/lib/ -lgsl -lgslcblas -lm test.o
test.o: In function `main':
test.cpp:(.text+0x1c): undefined reference to `gsl_sf_bessel_J0'
collect2: ld returned 1 exit status

But the libraries are available:

$ ll /usr/lib/libgsl*
lrwxrwxrwx 1 root root   16 Mar  2  2012 /usr/lib/libgsl.so.0 -> libgsl.so.0.16.0
lrwxrwxrwx 1 root root   16 Mar  2  2012 /usr/lib/libgsl.so -> libgsl.so.0.16.0
lrwxrwxrwx 1 root root   20 Mar  2  2012 /usr/lib/libgslcblas.so.0 -> libgslcblas.so.0.0.0
lrwxrwxrwx 1 root root   20 Mar  2  2012 /usr/lib/libgslcblas.so -> libgslcblas.so.0.0.0
-rw-r--r-- 1 root root 2.3M Mar  2  2012 /usr/lib/libgsl.so.0.16.0
-rw-r--r-- 1 root root 274K Mar  2  2012 /usr/lib/libgslcblas.so.0.0.0
-rw-r--r-- 1 root root 503K Mar  2  2012 /usr/lib/libgslcblas.a
-rw-r--r-- 1 root root 4.3M Mar  2  2012 /usr/lib/libgsl.a

As well as the header files:

$ whereis gsl
gsl: /usr/include/gsl /usr/share/man/man3/gsl.3.gz

I also tried downloading and installing the lib manually but there is no difference (this one is the Ubuntu package for 12.04 LTS).

/edit2:

Using nm it does not give any further hints:

$ nm /usr/lib/libgsl.a
[...]
bessel_J0.o:
00000000000004c0 T gsl_sf_bessel_J0
0000000000000000 T gsl_sf_bessel_J0_e
                 U gsl_sf_bessel_cos_pi4_e
[...]
like image 579
Oliver Avatar asked Sep 03 '13 09:09

Oliver


3 Answers

Easy fix:

You must link as follows

 g++ -L/usr/local/lib/ test.o -lgsl -lgslcblas -lm

You inverted the order while linking (first .o files, then the -l flags)

PS: I could reproduce your problem using your original

 g++ -L/usr/local/lib/ -lgsl -lgslcblas -lm test.o 

and I use gsl all the time without linking problem. I fixed by inverting the order as I said before.

PS2: enter image description hereSee picture

like image 153
Vivian Miranda Avatar answered Oct 19 '22 13:10

Vivian Miranda


This works without a hitch on Ubuntu 13.04:

edd@max:~/src/progs/C$ cat gsl_bessel.c
// cf http://stackoverflow.com/questions/18588607/g-unable-to-link-libgsl

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

int main (void) {
    double x = 5.0;
    double y = gsl_sf_bessel_J0 (x);
    printf ("J0(%g) = %.18e\n", x, y); 
    return 0;
}
edd@max:~/src/progs/C$ gcc -Wall -o gsl_bessel gsl_bessel.c -lgsl -lgslcblas -lm
edd@max:~/src/progs/C$ ./gsl_bessel 
J0(5) = -1.775967713143382642e-01
edd@max:~/src/progs/C$ 

Maybe reinstall / check the packages? They have not changed in a long while.

(Disclaimer: I happen to be the Debian maintainer behind these packages.)

Edit: Oh, re-reading your title and tags, your mistake is the use of g++ instead of gcc. This is a C library, and you wrote a C program. It works with C++ but you need extern "C" (which is a different topic).

Edit 2: Never mind. Works fine with g++ as well here as the headers are C++ ready:

edd@max:~/src/progs/C$ rm ./gsl_bessel
edd@max:~/src/progs/C$ g++ -Wall -o gsl_bessel gsl_bessel.c -lgsl -lgslcblas -lm
edd@max:~/src/progs/C$ ./gsl_bessel 
J0(5) = -1.775967713143382642e-01
edd@max:~/src/progs/C$ 
like image 35
Dirk Eddelbuettel Avatar answered Oct 19 '22 13:10

Dirk Eddelbuettel


GSL linking on Windows:

gcc -c test.c -Wall -I"C:\Program Files\GnuWin32\include"  -o test.o -L"C:\Program Files\GnuWin32\lib" -lgslcblas -lgsl -lm

gcc -o test.exe test.o -L"C:\Program Files\GnuWin32\lib" -lgslcblas -lgsl -lm

where C:\Program Files\GnuWin32\include" is location of include directory in GSL and "C:\Program Files\GnuWin32\lib" is location of lib directory.

like image 1
Vinod Kumar Chauhan Avatar answered Oct 19 '22 13:10

Vinod Kumar Chauhan