Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to add a relative library path to an executable to avoid setting LD_LIBRARY_PATH

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

like image 801
joeButler Avatar asked Sep 30 '14 22:09

joeButler


1 Answers

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.

like image 53
Amadeus Avatar answered Oct 21 '22 06:10

Amadeus