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

I am creating ffmpeg with librtmp. My librtmp is located in / opt / librtmp / lib. When I execute ffmpeg, he said:

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

I use the ldd command, it shows 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 like this:

 [ 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 dinner user
  • set the LD_LIBRARY_PATH variable, but it is not acceptable to users.
  • pass rpath to gcc for example

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" 

Suppose there is no source code to recompile? How to add a shared library search path to an executable?

+7
source share
4 answers

You can use addrpath to add RPATH to your elf file.

RPATH will work as LD_LIBRARY_PATH, that is, tell the dynamic loader to search for shared libraries along this path. RPATH will reside in your ELF file.

+3
source

I understand that the OP is probably moving on, but this is what NixOS does regularly, and they released a tool for this very problem. Also, it was a problem that I ran into even before I heard NixOS.

Here's an example of using their patchelf tool

... Similarly, you can change RPATH, the linker search path embedded in 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 the program ....

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

+3
source

My solution to this problem is to install librtmp in / usr / local / lib and run 'sudo ldconfig' after installing it. You can then configure Ffmpeg by simply adding --enable-librtmp. For me, this works great: no system changes are required!

+1
source

nixos

this one may be specific to nixos , but gives an interesting idea of ​​ldd / patchelf:

https://lastlog.de/blog/posts/playing_FTL_on_NIXOS.html

ubuntu

on ubuntu / fedora you should use: LD_LIBRARY_PATH with a ./ftl script ./ftl , again, see my post on FTL and how it is deployed above.

+1
source

All Articles