Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 858
ComplicatedPhenomenon Avatar asked Mar 14 '26 00:03

ComplicatedPhenomenon


2 Answers

Two options:

  1. (Static Library) When compiling write, for example:

    gcc -static -I $HOME/local/include -L $HOME/local/lib example.c -lgsl -lgslcblas -lm

and run it as:

./a.out
  1. (Dynamic Library) When compiling write, for example:

    gcc -I $HOME/local/include -L $HOME/local/lib example.c -lgsl -lgslcblas -lm

and to execute it:

export LD_LIBRARY_PATH=$HOME/local/lib

./a.out
like image 138
Jorge Sanabria Avatar answered Mar 15 '26 12:03

Jorge Sanabria


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.

like image 41
andres gomez Avatar answered Mar 15 '26 14:03

andres gomez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!