I have a program that wants to look for library files in a subdirectory of the directory I specify with an environment variable. Is it possible to put in my specified directory a path that ignores the first subdirectory that is being appended? Here's what I mean.

My folder structure

/usr/lib64/[All the library files]

Program looks at

$MAGICK_HOME/lib/[All the library files]

where I can manipulate $MAGICK_HOME.

So if I set MAGICK_HOME=/usr it will look at /usr/lib/[library files], which is wrong. If I set MAGICK_HOME=/usr/lib64 it will look at /usr/lib/lib64/[library files] which is also wrong.

If it were the other way round, I could ignore the last directory specified in the prefix path by starting my suffix with a ../ to get back from the outermost directory. What can I put as my MAGICK_HOME to specify the correct directory?


Here are 3 situations of interest:

Your app is reading the libraries of /usr/lib64, but you want it to read to libraries of $MAGICK_HOME/lib:

If you are compiling you application, you could add some RPATH or RUNPATH rules so that the binary searches for libs in $ORIGIN/../lib64. Otherwise, you could add $MAGICK_HOME/lib to $LD_LIBRARY_PATH in a shell script that starts your binary so the dynamic linker searches that path for binaries, or you could use ldconfig to add a specific library to /etc/ld.so.cache

Your app is reading the libraries of $MAGICK_HOME/lib instead of /usr/lib64

When ld.so dynamically links, it searches:

  • DT_RPATH field compiled in the ELF-binary (if DT_RUNPATH doesn't exist). This will usually be an absolute path, or a path relative to the location of the binary ($ORIGIN). I don't think an environment variable like $MAGICK_HOME can influence it.
  • $LD_LIBRARY_PATH environment variable (unless being run in secure-execution mode)
  • DT_RUNPATH field compiled in the ELF-binary (similar to DT_RPATH above).
  • /etc/ld.so.cache, which contains a compiled list of candidate shared objects.
  • default paths of /lib and /usr/lib. On some architetures, the default paths for 64-bit shared objects are /lib64 and /usr/lib64. If the binary was linked with the -z nodeflib linker option, this step is skipped.

Since /usr/lib64 is probably in the default path, I suspect the program is manipulating $LD_LIBRARY_PATH when it starts, or used ldconfig to add libraries into /etc/ld.so.cache during installation. This would cause the $MAGICK_HOME/lib libraries to be found first. If you can prevent it from doing these things, then it should fallback to /usr/lib64.

You can use readelf to see if it was compiled for a specific DT_RPATH or DT_RUNPATH, but I don't think that's the case because it sounds like an environment variable influences the linking and these options cannot (AFAIK) be influenced by the environment.

Your app is failing to start because it can't find the libraries installed in /usr/lib64

If the problem is not that it's loading the wrong binaries, but instead it's not loading any binaries, then it could be that /usr/lib64 is not in your default path. We'd need to know about your distro and architecture to help further in that case.

  • Thank you so much for the detailed answer. I think it may be a failing of the program that is trying to use the library, and not so much the location of the library files. I'm on CentOS trying to install Wand: docs.wand-py.org/en/0.6.6 . After installing ImageMagick-devel using yum and copying the library files to a place where I could specify the exact library directory I'm still having the same issue. I will add the trace to the question description. – Marcel Mar 3 at 17:26
  • Actually, it seems it has fixed itself, when looking back at the traces it seems there was another library interfering with imports. Now I get: DelegateError: Postscript delegate failed `[ghostscript library 9.25] -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r72x72' '-sOutputFile=/tmp/mrod/magick-16886b3woG5lrbXG3%d' '-f/tmp/mrod/magick-16886eW1PKJ1qhmUO' '-f/tmp/mrod/magick-16886EyTqaCf7AM7z'': (null) @ error/ghostscript-private.h/InvokeGhostscriptDelegate/171 – Marcel Mar 3 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.