How to change the paths to shared libraries (.so files) for a single terminal instance

26,643

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

Share:
26,643

Related videos on Youtube

Simon Streicher
Author by

Simon Streicher

Updated on September 18, 2022

Comments

  • Simon Streicher
    Simon Streicher over 1 year

    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.

    • Admin
      Admin almost 3 years
      If you want to test with another version of so while not messing with the binary. LD_PRELOAD trick might help. I know it's a bit off topic, but some people might need it while their search engine leads to here.
  • Simon Streicher
    Simon Streicher about 8 years
    If I am correctly reading this answer, then you have given me a tool to make my portable-isation robust against embedded paths in the library (thanks, willl have a look!). Or am I missing something? Will follow your links.
  • Marius
    Marius about 8 years
    yes - I've used this tool to remove paths (incorrectly) added by developers which interfere with building packages.