Reputation: 1698
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
Upvotes: 13
Views: 8277
Reputation: 100151
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]
Upvotes: 19
Reputation: 99374
Wrap your program in a shell script:
#!/bin/sh
PROGRAM_DIRECTORY="`dirname "$0"`"
export LD_LIBRARY_PATH="$PROGRAM_DIRECTORY"
"$PROGRAM_DIRECTORY/program_executable" "$@"
If you run this script (instead of your executable) your program will link just fine.
Upvotes: 2