mbohun
12/3/2012 - 1:11 AM

rpath, RPATH, readelf

rpath, RPATH, readelf

# Often you want to distributed/install alongside with your
# executable-s some (of yours or some 3rd party) shared libraries.
# In order for these shared libs to be found by the dynamic linker
# one has to either:
# - ask the user to manually set the LD_LIBRARY_PATH or
# - add tge dir with the shared libs to /etc/ld.so.conf OR
#   /etc/ld.so.conf.d/ (sys wide and requires root/sudo)
# - provide a wrapper shell startup script that sets LD_LIBRARY_PATH
#   or even preloads some shared libraries prior to running your
#   executable OR
# - use the so called rpath to encode the shared lib directories path
#   into your executable, the encoded rpath can use both absolute
#   and relative path-s (this is handy if you wish to
#   distribute/install your sharedlibs into a location relative to
#   your executable), here is an example:
# -Xlinker -R./some/path option makes gcc to pass the rpath option
# onto the linker, and as shown in the example bellow you can pass
# multiple of these
#
$ gcc test_rpath.c -Xlinker -R./plugins -Xlinker -R/opt/test/lib

# you may use readelf to verify/examine (if and how) RPATH got encoded in your executable (or shared lib)
#
$ readelf -a a.out | grep -i rpath
 0x0000000f (RPATH)                      Library rpath: [./plugins:/opt/test/lib]
    38: 00000000     0 FILE    LOCAL  DEFAULT  ABS test_rpath.c