/* Ajith - Syntax Higlighter - End ----------------------------------------------- */


Creating Shared Libraries in Linux - Part 2

Check the PART1 of this article.

4. Making the library available at run-time

We have to create or set the environment variable "LD_LIBRARY_PATH" to the directory containing the shared libraries.
export LD_LIBRARY_PATH=/home/cf/lib
If in current directory you can give the following command
If we have to append a new directory to the existing paths then add the directories separated by colons to environment variable "LD_LIBRARY_PATH".
Now recompile the main program
gcc -o test main.c -lcalc_mean -L/home/cf/slib
Now check the ldd command output
$ ldd test
 linux-gate.so.1 =>  (0x007ad000)
 libcalc_mean.so => ./libcalc_mean.so (0x0081e000)
 libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x005ff000)
 /lib/ld-linux.so.2 (0x00e19000)
It seems now linker is able to locate our shared library as we can see in above output. Now run the program
LD_LIBRARY_PATH is good for quick tests and for systems on which you don’t have admin privileges. As a downside, however, exporting the LD_LIBRARY_PATH environment variable  might screw up with other programs you run that also rely on LD_LIBRARY_PATH if you don’t reset it to its previous state when you’re done.

Using rpath
Rpath, or the run path or -R, is a way of embedding the location of shared libraries in the executable itself, instead of relying on default locations or environment variables. We do this during the linking stage. Before we compile the main.c with rpath option we will unset the LD_LIBRARY_PATH.
gcc -g -o test main.c -lcalc_mean -L/home/cf/slib -Wl,-R/home/cf/slib
gcc -g -o test main.c -lcalc_mean -L/home/cf/slib -Wl,-rpath=/home/cf/slib
  • -Wl portion sends comma-separated options to the linker, so we tell it to send the -rpath option to the linker with our working directory.
$ ldd test
 linux-gate.so.1 =>  (0x00a1b000)
 libcalc_mean.so => /home/cf/slib/libcalc_mean.so (0x00e84000)
 libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x00359000)
 /lib/ld-linux.so.2 (0x007c3000)
The rpath method is great since each program gets to list its shared library locations independently, so there are no issues with different programs looking in the wrong paths like there were for LD_LIBRARY_PATH. But rpath has its downsides even.
  • First, it requires all shared libraries be installed in a fixed location so that all users of your program will have access to those libraries in those locations. That means less flexibility in system configuration. 
  • Second, if that library refers to a NFS mount or other network drive, you may experience undesirable delays–or worse–on program startup.

1. Yolinux
2. CProgramming