arunabh sharma
arunabh sharma

Reputation: 67

Library not linked even though present in link targets

I'm trying to build an application using cmake but the executable is unable to link to the libraries, the ldd of my executable looks like this

linux-vdso.so.1 (0x00007ffd447a0000)
libavcodec.so.58 => /usr/local/ffmpeg/libavcodec.so.58 (0x00007f5673121000)
libavformat.so.58 => /usr/local/ffmpeg/libavformat.so.58 (0x00007f5672cdb000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f5671ffb000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f5671de3000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f56719f2000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f5671654000)
/lib64/ld-linux-x86-64.so.2 (0x00007f567629b000)
libswresample.so.3 => not found
libavutil.so.56 => not found
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f5671435000)
libavutil.so.56 => not found
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f5671231000)

while in my make --trace the library is present

/usr/bin/c++   -I/home/arunabh-compute/sensor-drivers -isystem 
/usr/local/include -isystem /usr/local/GenICam/include  -Wall -O3   - 
std=gnu++14 -o CMakeFiles/save_cpp.dir/stdafx.cpp.o -c /home/arunabh- 
compute/sensor-drivers/stdafx.cpp
CMakeFiles/save_cpp.dir/build.make:178: update target 'save_cpp' due 
to: CMakeFiles/save_cpp.dir/link.txt 
CMakeFiles/save_cpp.dir/Cpp_Save.cpp.o 
CMakeFiles/save_cpp.dir/stdafx.cpp.o 
CMakeFiles/save_cpp.dir/build.make /usr/local/ffmpeg/libavcodec.so 
/usr/local/ffmpeg/libavcodec.so.58 
/usr/local/ffmpeg/libavcodec.so.58.18.100 
/usr/local/ffmpeg/libavdevice.so /usr/local/ffmpeg/libavdevice.so.58 
/usr/local/ffmpeg/libavdevice.so.58.3.100 
/usr/local/ffmpeg/libavfilter.so /usr/local/ffmpeg/libavfilter.so.7 
/usr/local/ffmpeg/libavfilter.so.7.16.100 
/usr/local/ffmpeg/libavformat.so /usr/local/ffmpeg/libavformat.so.58 
/usr/local/ffmpeg/libavformat.so.58.12.100 
/usr/local/ffmpeg/libavutil.so /usr/local/ffmpeg/libavutil.so.56 
/usr/local/ffmpeg/libavutil.so.56.14.100 
/usr/local/ffmpeg/libswresample.so 
/usr/local/ffmpeg/libswresample.so.3 
/usr/local/ffmpeg/libswresample.so.3.1.100 
/usr/local/ffmpeg/libswscale.so /usr/local/ffmpeg/libswscale.so.5 
/usr/local/ffmpeg/libswscale.so.5.1.100 

even though libswresample.so.3, libavutil.so.56 is clearly present in the make --trace the application doesn't link to it for some reason, additionally if i put the /usr/local/ffmpeg folder in my LD_LIBRARY_PATH everything starts working just fine. Additionally libavcodec.so.58 is found. Trying to understand what's happening and how to fix it. Thanks.

Upvotes: 1

Views: 647

Answers (1)

Botje
Botje

Reputation: 31010

By linking with these libraries, you marked them as needed in your binary, without recording any paths.

By default, your system only looks for dynamic libraries in a handful of locations. You have a few options:

  • Add /usr/local/ffmpeg to a file in /etc/ld.so.conf.d/ (don't forget to call ldconfig). This will tell your dynamic linker (ld.so) to look there.
  • Add /usr/local/ffmpeg to the LD_LIBRARY_PATH environment variable as you did. You can make this permanent by editing /etc/environment on some machines. This has the same effect, but can be overridden by users. Most games on Linux rely on this to find the bundled libraries, for example, but this does necessitate a launcher script.
  • Link your binary with -rpath=/usr/local/ffmpeg. This informs the dynamic linker that you would like it to look there too. You can do this in CMake by setting the INSTALL_RPATH target property. You probably want to set BUILD_WITH_INSTALL_RPATH as well, so you do not have to set LD_LIBRARY_PATH during development or set a separate BUILD_RPATH.
  • Finally, you can just move the ffmpeg libraries to a directory in the default search path. However, this may clash with system libraries and requires root access. This is more a last-resort or convenience-over-everything approach.

Upvotes: 0

Related Questions