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

Viewed 10646

After installing GNU Scientific Library (GSL) from source code, I write a program to test if the library work.

 $gcc -Wall -I ~/gsl/include -c example.c
 $gcc -L ~/gsl/lib example.o -lgsl -lgslcblas -lm
 $./a.out 
 ./a.out: error while loading shared libraries: libgsl.so.23: cannot open shared object file: No such file or directory

There is no problem in Compiling and linking procedure. No such file or directory but there is such directory.

  $ls -l ~/gsl/lib
total 32920
-rw-r--r-- 1 wm wm 19418210 8月  14 07:09 libgsl.a
-rw-r--r-- 1 wm wm  1840642 8月  14 07:08 libgslcblas.a
-rwxr-xr-x 1 wm wm      951 8月  14 07:08 libgslcblas.la
lrwxrwxrwx 1 wm wm       20 8月  14 07:08 libgslcblas.so -> libgslcblas.so.0.0.0
lrwxrwxrwx 1 wm wm       20 8月  14 07:08 libgslcblas.so.0 -> libgslcblas.so.0.0.0
-rwxr-xr-x 1 wm wm  1100520 8月  14 07:08 libgslcblas.so.0.0.0
-rwxr-xr-x 1 wm wm      920 8月  14 07:09 libgsl.la
lrwxrwxrwx 1 wm wm       16 8月  14 07:09 libgsl.so -> libgsl.so.23.0.0
lrwxrwxrwx 1 wm wm       16 8月  14 07:09 libgsl.so.23 -> libgsl.so.23.0.0
-rwxr-xr-x 1 wm wm 11333224 8月  14 07:09 libgsl.so.23.0.0
drwxrwxr-x 2 wm wm     4096 8月  14 07:09 pkgconfig
4 Answers

I may be a bit late for the question, but I hope this answer can help someone else who's suffering the same problem.

That's probably because your gcc don't know where the shared library is. Actually, you have to explicitly tell your compiler the location of the shared library that is used by your program. The solutions being: Plan A(temporary): In bash, you need

export LD_LIBRARY_PATH=/usr/local/lib

supposing your gsl is installed in the default directory. Plan B(permanent): edit /etc/ld.so.conf (you need sudo this). Put your location at the end of the file:

sudoedit /etc/ld.so.conf

In vim, type o and then /usr/local/lib. Then type esc and type :wq.

Now recompile your file and it will work. If not, you can run ldd a.out to see if the libraries are linked.

I know that there is little late, but I would like to complement the answer. First, make sure that the local installation that you do is correct by following the steps in https://coral.ise.lehigh.edu/jild13/2016/07/11/hello/. Then, you can compile all in one line as follows:

gcc -Wall -I/path_to_include/ -L/path_to_lib/ program_name.c -o output_name.out -lgsl -lgslcblas -lm

Also, for this to work properly you need to include the library path in the ~/.bashrc as follows:

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/path_to_lib/"

So, explaining a little: -Wall is for the warning as is widely known. The -I leads to the .h files, which have defined functions and variables needed, and the -L option is for the compiler to be able to know where are the shared libraries. Finally, the -lm and so on options are for the compiler to know to which libraries it must link the output. The fact that the path must be in the .bashrc comes from the fact that the libraries are used dynamically, and so at the run time it must be able to find that path, which is loaded thanks to the bashrc wh.

Related