I'm trying to run 2 simple examples in c/c++ using the GNU Scientific Library.

That are the codes:

1st: An Example Program

#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;

2nd: Example programs for matrices

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

int main (void)
  int i, j; 
  gsl_matrix * m = gsl_matrix_alloc (10, 3);

  for (i = 0; i < 10; i++)
    for (j = 0; j < 3; j++)
      gsl_matrix_set (m, i, j, 0.23 + 100*i + j);

  for (i = 0; i < 100; i++)  /* OUT OF RANGE ERROR */
    for (j = 0; j < 3; j++)
      printf ("m(%d,%d) = %g\n", i, j, 
              gsl_matrix_get (m, i, j));

  gsl_matrix_free (m);

  return 0;

I saw on the Software Center that I have the GNU Scientific Library (GSL) - Library package (libgsl0ldbl 1.16+dfsg-1ubuntu1) but the documentation that I read said that I needed to compile with the next code:

$ gcc -Wall -I/usr/local/include -c example.c

And directory /usr/local/include was empty so recently I have also installed manually from the site Main GSL - GNU Scientific Library site the new version GSL2.1. Next I run the code before mentioned so I had a new file example.o then I run the next code:

$ gcc -L/usr/local/lib example.o -lgsl -lgslcblas -lm

So I had a a.out file, I tried to run $ ./a.out but the output was:

`./a.out: error while loading shared libraries: libgsl.so.19: cannot open shared object file: No such file or directory`

I tried this command too:

$ ldconfig -p | grep gsl
    libgslcblas.so.0 (libc6,x86-64) => /usr/lib/libgslcblas.so.0
    libgsl.so.0 (libc6,x86-64) => /usr/lib/libgsl.so.0

So I can see that the excecution of a.out was asking for libgsl.so.19 but the ldconfig said that the library on the pc is libgsl.so.19

  • 1
    Ubuntu 16.04 has libgsl.so.19 in the libgsl2 package. But if you've installed manually, check if sudo ldconfig helps to update the library lookup cache or try to specify the search path manually when launching: LD_LIBRARY_PATH=/usr/local/lib ./a.out – Velkan May 1 '16 at 21:13
  • Try: sudo apt-get install gsl-bin and then gcc example.c -lgsl -lgslcblas -lm – Intrus May 1 '16 at 21:19
  • Thank you guys! @Velkan, I can solve this problem with sudo ldconfig. – Adolfo Correa May 2 '16 at 18:01

you can set the path of library and exporting it, this is a dirty way but it works

  1. LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/data2/Irfan/gsl/lib
  2. export LD_LIBRARY_PATH

note* /data2/Irfan/gsl/lib is a path of where you have installed gsl

| improve this answer | |

I guess your problem might have been solved now. This answer will provide easy reference for anyone who might have faced the similar issue.

Apart from Irfan solution which is a perfectly valid solution. Another option is to link static library at the time of linking.

gcc -L/usr/local/lib example.o -l:libgsl.a -lgslcblas -lm

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.