Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add shared library search path to a executable file?

I build the ffmpeg with librtmp. My librtmp is at /opt/librtmp/lib. When I execute the ffmpeg, it said:

./ffmpeg: error while loading shared libraries: librtmp.so.0: cannot open shared object file: No such file or directory

I use ldd command it displays not found:

[qty@testing bin]# ldd ffmpeg 
        linux-vdso.so.1 =>  (0x00007fff15576000)
        librtmp.so.0 => not found
        libz.so.1 => /lib64/libz.so.1 (0x00002b9a71e10000)
        libm.so.6 => /lib64/libm.so.6 (0x00002b9a72025000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00002b9a722a8000)
        libc.so.6 => /lib64/libc.so.6 (0x00002b9a724c3000)
        /lib64/ld-linux-x86-64.so.2 (0x00002b9a71bf2000)

I know my so at:

[qty@testing bin]# ls -alh  /opt/librtmp/lib/
total 300K
drwxr-xr-x 3 root root 4.0K Sep 25 17:10 .
drwxr-xr-x 7 root root 4.0K Sep 25 17:10 ..
-rw-r--r-- 1 root root 158K Sep 25 17:10 librtmp.a
lrwxrwxrwx 1 root root   12 Sep 25 17:10 librtmp.so -> librtmp.so.0
-rwxr-xr-x 1 root root 118K Sep 25 17:10 librtmp.so.0
drwxr-xr-x 2 root root 4.0K Sep 25 17:10 pkgconfig

I found several ways to fix the problem

  • modify /etc/ld.so.conf, but it required a supper user
  • set LD_LIBRARY_PATH variable, but it is not conventient to users
  • pass rpath to gcc, like this

configure args for my ffmpeg

PKG_CONFIG_PATH="/opt/librtmp/lib/pkgconfig" ./configure --disable-doc \
--disable-ffserver --disable-avdevice \
--disable-postproc --disable-avfilter --disable-bsfs \
--disable-filters \
--disable-asm \
--disable-bzlib \
--enable-librtmp \
--prefix=/opt/ffmpeg \
--extra-ldflags="-Wl,-rpath,/opt/librtmp/lib"

Assume there are no source code to re-compile? How do add the shared library search path to a executable file ?

like image 422
qrtt1 Avatar asked Sep 25 '12 09:09

qrtt1


People also ask

Is a shared library an executable?

Static Libraries are linked into a compiled executable (or another library). After the compilation, the new artifact contains the static library's content. Shared Libraries are loaded by the executable (or other shared library) at runtime.

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 .

Where do executables look for shared objects at runtime?

The default directories, normally /lib and /usr/lib. 8. For a native linker on an ELF system, if the file /etc/ld.


Video Answer


2 Answers

I realize that OP has probably moved on but this is the kind of thing that NixOS does regularly and they have released a tool for this very problem. Also this was a problem I had before even hearing of NixOS.

Here's an example usage of their tool patchelf

... Likewise, you can change the RPATH, the linker search path embedded into executables and dynamic libraries:

patchelf --set-rpath /opt/my-libs/lib:/foo/lib program

This causes the dynamic linker to search in /opt/my-libs/lib and /foo/lib for the shared libraries needed by program....

From https://nixos.org/patchelf.html

like image 168
EdgarArout Avatar answered Oct 28 '22 03:10

EdgarArout


You could use addrpath to add an RPATH to your elf file.

The RPATH will work like LD_LIBRARY_PATH, that is, telling the dynamic loader to search for the shared libraries in that path. RPATH will be permanently in your ELF file.

like image 20
JohnTortugo Avatar answered Oct 28 '22 02:10

JohnTortugo