Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force using local shared libraries over system libraries?

How can I force using a local library over the system library in linux?

I linked my executable explicitly to some .so files in my project/lib directory e.g. (../lib/libluajit.so).

Running my executable under gdb or using ldd shows that it still uses the system libluajit-5.1.so.2

I then set LD_LIBRARY_PATH to my project/lib directory and exported it, then ran my executable. Somehow it's still picking up the system library (confirmed by both gdb and ldd)

I'd like to know how that's even possible, and what I can do to force it to use the local libluajit.so in my project/lib directory.

like image 876
Eloff Avatar asked Nov 13 '12 19:11

Eloff


People also ask

Are system libraries always shared?

Most libraries are shared, but if you do something unusual, such as moving a shared library to /lib accidentally breaking the . so symlink in /usr/lib while keeping the static library in /lib , the static library will be silently linked into the programs which need it.

Do shared libraries need executable?

As shared libraries cannot be directly executed, they need to be linked into a system executable or callable shared object. Hence, shared libraries are searched for by the system linker during the link process. This means that a shared library name must always start with the prefix lib and have the extension .

How are shared libraries loaded in Linux?

Shared libraries are the most common way to manage dependencies on Linux systems. These shared resources are loaded into memory before the application starts, and when several processes require the same library, it will be loaded only once on the system. This feature saves on memory usage by the application.

How are shared libraries linked?

Shared libraries (also called dynamic libraries) are linked into the program in two stages. First, during compile time, the linker verifies that all the symbols (again, functions, variables and the like) required by the program, are either linked into the program, or in one of its shared libraries.


1 Answers

When you link, specify the directory of the library and also use an rpath:

-Wl,-rpath,/absolute/path/to/your/library -L/absolute/path/to/your/library -llibrary

-L tells the linker where to find your library at link time, and -rpath tells it where to search for the library at runtime.

Note that -L and -rpath need the directory that contains your .so file, not the actual path of the library file itself.

like image 188
Nikos C. Avatar answered Sep 21 '22 13:09

Nikos C.