Next: , Previous: Compiling and Linking, Up: Using the library


2.3 Shared Libraries

To run a program linked with the shared version of the library the operating system must be able to locate the corresponding .so file at runtime. If the library cannot be found, the following error will occur:

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

To avoid this error, either modify the system dynamic linker configuration1 or define the shell variable LD_LIBRARY_PATH to include the directory where the library is installed.

For example, in the Bourne shell (/bin/sh or /bin/bash), the library search path can be set with the following commands:

     $ LD_LIBRARY_PATH=/usr/local/lib
     $ export LD_LIBRARY_PATH
     $ ./example

In the C-shell (/bin/csh or /bin/tcsh) the equivalent command is,

     % setenv LD_LIBRARY_PATH /usr/local/lib

The standard prompt for the C-shell in the example above is the percent character ‘%’, and should not be typed as part of the command.

To save retyping these commands each session they can be placed in an individual or system-wide login file.

To compile a statically linked version of the program, use the -static flag in gcc,

     $ gcc -static example.o -lgsl -lgslcblas -lm

Footnotes

[1] /etc/ld.so.conf on GNU/Linux systems.