Shared Libraries – Change Paths for Single Terminal Instance

dependencieslibrarieslinkershared library

I want to remove some of the paths the linker uses to find .so libraries for testing purposes.

I have found a way to add library paths:

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/path/to/library"

Are there a variable similar to LD_LIBRARY_PATH that I can use to remove library paths such as /usr/local/lib or /usr/lib that are not in LD_LIBRARY_PATH but picked up by the linker? I.e. how can I ignore paths that are given in /etc/ld.so.conf.d/ ?

The reason for this is that I am busy creating a program that, for a given executable, it recursively finds library dependencies. I want to see if I can make a program more portable by finding all its dependencies, copying those dependencies into a local directory, and make a local-run bash script to setup LD_LIBRARY_PATH and then run the executable. I want to test if this local-run-executable works after previously important library search paths are removed.

Best Answer

You would be interested in removing library paths if a given shared library has embedded paths via the rpath feature. Those are added at the time the library is created by the linker.

You can remove (or alter) those paths using chrpath, e.g.,

chrpath -d mylibraryfile.so

Removing pathnames from the LD_LIBRARY_PATH variable also is a possible area of interest; you can do that by string substitution and re-exporting the variable. However, the question does not seem to be concerned with that. There is no variable which acts to cancel out LD_LIBRARY_PATH.

For seeing library dependencies, the mention of /etc/ld.so.conf.d/ makes it sound as if the platform is only Linux. You can use ldd to list dependencies. Aside from OSX, all of the BSDs also support ldd. Here is one of the scripts which I use for this purpose:

#!/bin/sh
# $Id: ldd-path,v 1.1 2007/07/09 19:30:28 tom Exp $
# Edit the output of ldd for the given parameters, yielding only the
# absolute pathnames.
ldd $* | sed \
        -e 's/([^)]*)//g' \
        -e 's/^.*=>//' \
        -e 's/[         ][      ]*//g' \
        -e '/^$/d'

But (addressing a comment), there is no portable mechanism for telling the loader to ignore an existing path. The GNU ld documentation gives a summary of what is sought, and the order in the description of the -rpath option. These items conclude the list:

  • The default directories, normally /lib and /usr/lib.
  • For a native linker on an ELF system, if the file /etc/ld.so.conf exists, the list of directories found in that file.

Further reading

Related Question