7
votes

I'm building a program which links some shared libraries. They are contained in a lib/ directory relative to my project.

The problem I'm having is that I'd like the executable to know to search for the libraries in the relative ../lib directory. Is this possible?

I don't really want to change ld_library_path or move the files into one of the root owned standard paths.

I'm able to compile the executable and run it when the .so files are on one of the standard paths shown with strace/ldd

1
can't you run the program from a script that temporarily changes the library load pathsCheers and hth. - Alf
Yeah that would be an option if all else fails. I could create a wrapper which does this from the Makefile. Eventually I'd like to be using Boost::Python to create a python module which would not work with this method. Just looking for options before I end up coping things into /usr/libjoeButler

1 Answers

8
votes

You can use the -rpath from the linker (using ld as the linker).

From its manual page:

-rpath=dir

Add a directory to the runtime library search path. This is used when linking an ELF executable with shared objects. All -rpath arguments are concatenated and passed to the runtime linker, which uses them to locate shared objects at runtime. The -rpath option is also used when locating shared objects which are needed by shared objects explicitly included in the link; see the description of the -rpath-link option. If -rpath is not used when linking an ELF executable, the contents of the environment variable "LD_RUN_PATH" will be used if it is defined.