Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared Library Path as Executable Directory

I have an application that is broken into several libraries for purposes of code reuse. On Windows all I have to do is put the .dll files in the same path as the executable and it automatically finds them. On Linux (since it hardcodes the paths to things) I have to specify the environmental variable LD_LIBRARY_PATH or preload the libraries before the executable.

I've seen some things about embedding the path using the linker option of -Wl,-rpath=<PATH> and I've tried it using . as the path. But that just looks in the current working directory, not the executable's directory.

Is there a way to specify in the linker to look in the directory of the executable for the shared libraries by default (like on Windows)?

Thanks! Matt

like image 677
CuppM Avatar asked Nov 23 '09 20:11

CuppM


People also ask

Is a shared library an executable?

Shared library are the one which do some task that is commonly accessed or used by many executables. These library are loaded into the memory only once and accessed by many programs(executables) at runtime.

Why are shared libraries executable?

The executable mode is a convention that gives the OS another level of access rights control. The executable loader controls that access, to ensure the user can execute it, but also to prevent errors (some scripts or programs should not be executed by some people).

Can you execute a shared library?

Shared libraries and Executables are pretty much the same thing ( ELF binaries). Except that shared libs have no fixed entry-point address while executables do. Also shared libs are PIE while binaries are not by default.

Is a shared object an executable?

There's no difference, aside from the fact that a compiled executable might be linked against a shared object but not against an executable.


1 Answers

You need $ORIGIN in your RPATH, via an appropriate option to ld or other Darwin tool. See this and this.

Remember that the $ has to really end up in the path, so you need to quote or escape it in the link command line.

Update: You can see what the linker actually put into your executable with

readelf -d /path/to/exe | grep RPATH

Here is what the output should look like:

 0x0000000f (RPATH)              Library rpath: [$ORIGIN]
like image 52
bmargulies Avatar answered Sep 29 '22 10:09

bmargulies