3

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
ComplicatedPhenomenon
  • 4,055
  • 2
  • 18
  • 45
  • What answer does this give? "echo $LD_LIBRARY_PATH" - shouldn't be blank. – Rob Aug 14 '17 at 00:01
  • @Rob, It's blank, what can be done now? – ComplicatedPhenomenon Aug 14 '17 at 00:12
  • Depending upon your flavor of Linux it should point at your System Library, there should also be a USR Library (assuming you've installed something that you have compiled previously). Unfortunately I am busy for an hour, check this out while you wait (though someone else will likely show up shortly): https://stackoverflow.com/questions/480764/linux-error-while-loading-shared-libraries-cannot-open-shared-object-file-no-s -- if you're familiar with Linux that should be enough direction, if not we'll get you up and running. Will need the version of Linux to give correct Path. See you soon. – Rob Aug 14 '17 at 00:24
  • @Rob, It's helpful, thanks so much. – ComplicatedPhenomenon Aug 14 '17 at 00:36

4 Answers4

3

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
  • use `g++` instead of `gcc`, when linking, the `gcc` command just sees a bunch of `.o` files, the `g++` command differs from the `gcc` command in that it knows where to find the c++ specific libraries – jk2K Dec 24 '17 at 04:20
2

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.

1

Read: https://www.gnu.org/software/gsl/doc/html/usage.html#shared-libraries

Generally you will want to build GNU Software by typing:

./configure [optional necessary cmd line arguments]

make

make check

make install

You've done the first two lines so just do the check and install - then ./a.out should run.

Also see: http://www.cnblogs.com/emanlee/p/3318337.html

Rob
  • 1,487
  • 2
  • 25
  • 29
  • 1
    actually, I had done the check and install steps. Then I test to see if the library work. and that error `./a.out: error while loading shared libraries: libgsl.so.23: cannot open shared object file: No such file or directory` appeared. – ComplicatedPhenomenon Aug 14 '17 at 01:51
  • 2
    Then I do `LD_LIBRARY_PATH=/usr/loca/lib`, `LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/gsl/lib/` and `export LD_LIBRARY_PATH`. Then the library work. – ComplicatedPhenomenon Aug 14 '17 at 02:15
  • If the answer was correct you can UpVote it so others will know. – Rob Aug 18 '17 at 15:47
1

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.

Vicente Bolea
  • 1,409
  • 16
  • 39
omnijust
  • 11
  • 1
  • 1