How to hardcode a dynamic library path in Linux

I would like to hardcode the library path in my Linux executable. On OS X, this is achieved by providing the full path after the -o argument when creating the library. For example, I create such a library on OS X:

cc foo.c --shared -o /home/sander/libfoo.so

When I create an executable file called "bar" that links to this library, and I use otool -L in the executable, I get this output:

bar:
    /home/sander/libfoo.so (compatibility version 0.0.0, current version 0.0.0)

Now I can run this executable from anywhere, and it can always find the library.

I am looking for equivalent functionality on Linux with gcc. I would prefer not to use rpath, as it does not reference a specific library + path.

+4
source share
1

, -llib, :

cd /full/path/to/lib
gcc -shared -fpic -o liblib.so lib.c             # make the lib
gcc -c -o prog.o prog.c                          # compile program
gcc -o prog prog.o "/full/path/to/lib/liblib.so" # link everything together

EDIT: , OS X , -o. . "" Mach-O LC_ID_DYLIB. @Sander Mertens , .

+4

All Articles