Issue
I am building couple of libs in ubuntu using CMake, and I noticed whenever I try to use the files in the installed directory, I get missing libs error. using ldd
I noticed in these files, the libs are not found while if I go back to the original file built, it has all the references and copying it to the installation directory fixes the issue.
To make all this more clear consider the following CMakeList.txt that I use to build my library:
cmake_minimum_required(VERSION 3.11)
project(AntiSpoofer)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_INSTALL_PREFIX /home/me/Desktop/LibtorchPort/built_stuff)
add_definitions(-D_ANTISPOOFER_BUILD_DLL)
find_package(Torch REQUIRED)
find_package(OpenCV REQUIRED)
include_directories( /home/me/Desktop/LibtorchPort/Dependencies/include ${TORCH_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS})
set(ANTISPOOFER_SRC ./AntiSpoofer.cpp )
add_library(
AntiSpoofer_dynamic
SHARED
${ANTISPOOFER_SRC}
)
target_link_directories(AntiSpoofer_dynamic PUBLIC /home/me/Desktop/LibtorchPort/Blinker/build)
set(LIBS ${LIBS} ${TORCH_LIBRARIES} ${OpenCV_LIBS} Blinker)
# Link
target_link_libraries(AntiSpoofer_dynamic ${LIBS})
install(TARGETS AntiSpoofer_dynamic DESTINATION lib)
This is the way I call my cmake :
cmake -DCMAKE_PREFIX_PATH="$(python -c 'import torch.utils; print(torch.utils.cmake_prefix_path)')" ..
and do a make install afterward.
Now when I navigate to AntiSpoofer/build
and get an MD5 hash on the created object this is what I get:
and the output of ldd libAntiSpoofer_dynamic.so
is as follows:
linux-vdso.so.1 (0x00007ffc23bf6000)
libtorch.so => /home/hossein/anaconda3/lib/python3.8/site-packages/torch/lib/libtorch.so (0x00007f5cd35f2000)
libBlinker.so => /home/hossein/Desktop/LibtorchPort/Blinker/build/libBlinker.so (0x00007f5cd355e000)
libtorch_cpu.so => /home/hossein/anaconda3/lib/python3.8/site-packages/torch/lib/libtorch_cpu.so (0x00007f5cc36af000)
libopencv_core.so.3.4 => /usr/local/lib/libopencv_core.so.3.4 (0x00007f5cc26d4000)
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f5cc24dc000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f5cc24bf000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5cc22cd000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f5cc22aa000)
libc10.so => /home/hossein/anaconda3/lib/python3.8/site-packages/torch/lib/libc10.so (0x00007f5cc2026000)
libopencv_highgui.so.3.4 => /usr/local/lib/libopencv_highgui.so.3.4 (0x00007f5cc200d000)
libopencv_imgproc.so.3.4 => /usr/local/lib/libopencv_imgproc.so.3.4 (0x00007f5cc048c000)
libcblas.so.3 => /lib/x86_64-linux-gnu/libcblas.so.3 (0x00007f5cc0465000)
liblapack.so.3 => /lib/x86_64-linux-gnu/liblapack.so.3 (0x00007f5cbfd9d000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f5cbfc4e000)
/lib64/ld-linux-x86-64.so.2 (0x00007f5cd3844000)
libgomp-7c85b1e2.so.1 => /home/hossein/anaconda3/lib/python3.8/site-packages/torch/lib/libgomp-7c85b1e2.so.1 (0x00007f5cbfa24000)
libtensorpipe.so => /home/hossein/anaconda3/lib/python3.8/site-packages/torch/lib/libtensorpipe.so (0x00007f5cbf55b000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f5cbf550000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f5cbf548000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f5cbf52c000)
libgtk-3.so.0 => /lib/x86_64-linux-gnu/libgtk-3.so.0 (0x00007f5cbed7b000)
libgdk-3.so.0 => /lib/x86_64-linux-gnu/libgdk-3.so.0 (0x00007f5cbec76000)
libcairo.so.2 => /lib/x86_64-linux-gnu/libcairo.so.2 (0x00007f5cbeb53000)
libgdk_pixbuf-2.0.so.0 => /lib/x86_64-linux-gnu/libgdk_pixbuf-2.0.so.0 (0x00007f5cbeb29000)
libgobject-2.0.so.0 => /lib/x86_64-linux-gnu/libgobject-2.0.so.0 (0x00007f5cbeac9000)
libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f5cbe9a0000)
libopencv_imgcodecs.so.3.4 => /usr/local/lib/libopencv_imgcodecs.so.3.4 (0x00007f5cbe85b000)
libatlas.so.3 => /lib/x86_64-linux-gnu/libatlas.so.3 (0x00007f5cbe4cf000)
libblas.so.3 => /lib/x86_64-linux-gnu/libblas.so.3 (0x00007f5cbe107000)
libgfortran.so.5 => /lib/x86_64-linux-gnu/libgfortran.so.5 (0x00007f5cbde43000)
libgmodule-2.0.so.0 => /lib/x86_64-linux-gnu/libgmodule-2.0.so.0 (0x00007f5cbde3d000)
libpangocairo-1.0.so.0 => /lib/x86_64-linux-gnu/libpangocairo-1.0.so.0 (0x00007f5cbde2b000)
libX11.so.6 => /lib/x86_64-linux-gnu/libX11.so.6 (0x00007f5cbdcee000)
libXi.so.6 => /lib/x86_64-linux-gnu/libXi.so.6 (0x00007f5cbdcdc000)
libXfixes.so.3 => /lib/x86_64-linux-gnu/libXfixes.so.3 (0x00007f5cbdcd2000)
libcairo-gobject.so.2 => /lib/x86_64-linux-gnu/libcairo-gobject.so.2 (0x00007f5cbdcc6000)
libatk-1.0.so.0 => /lib/x86_64-linux-gnu/libatk-1.0.so.0 (0x00007f5cbdc9c000)
libatk-bridge-2.0.so.0 => /lib/x86_64-linux-gnu/libatk-bridge-2.0.so.0 (0x00007f5cbdc65000)
libepoxy.so.0 => /lib/x86_64-linux-gnu/libepoxy.so.0 (0x00007f5cbdb32000)
libfribidi.so.0 => /lib/x86_64-linux-gnu/libfribidi.so.0 (0x00007f5cbdb15000)
libgio-2.0.so.0 => /lib/x86_64-linux-gnu/libgio-2.0.so.0 (0x00007f5cbd932000)
libpangoft2-1.0.so.0 => /lib/x86_64-linux-gnu/libpangoft2-1.0.so.0 (0x00007f5cbd919000)
libpango-1.0.so.0 => /lib/x86_64-linux-gnu/libpango-1.0.so.0 (0x00007f5cbd8ca000)
libharfbuzz.so.0 => /lib/x86_64-linux-gnu/libharfbuzz.so.0 (0x00007f5cbd7c5000)
libfontconfig.so.1 => /lib/x86_64-linux-gnu/libfontconfig.so.1 (0x00007f5cbd77e000)
libfreetype.so.6 => /lib/x86_64-linux-gnu/libfreetype.so.6 (0x00007f5cbd6bf000)
libXinerama.so.1 => /lib/x86_64-linux-gnu/libXinerama.so.1 (0x00007f5cbd6b8000)
libXrandr.so.2 => /lib/x86_64-linux-gnu/libXrandr.so.2 (0x00007f5cbd6ab000)
libXcursor.so.1 => /lib/x86_64-linux-gnu/libXcursor.so.1 (0x00007f5cbd69e000)
libXcomposite.so.1 => /lib/x86_64-linux-gnu/libXcomposite.so.1 (0x00007f5cbd699000)
libXdamage.so.1 => /lib/x86_64-linux-gnu/libXdamage.so.1 (0x00007f5cbd694000)
libxkbcommon.so.0 => /lib/x86_64-linux-gnu/libxkbcommon.so.0 (0x00007f5cbd650000)
libwayland-cursor.so.0 => /lib/x86_64-linux-gnu/libwayland-cursor.so.0 (0x00007f5cbd645000)
libwayland-egl.so.1 => /lib/x86_64-linux-gnu/libwayland-egl.so.1 (0x00007f5cbd640000)
libwayland-client.so.0 => /lib/x86_64-linux-gnu/libwayland-client.so.0 (0x00007f5cbd62f000)
libXext.so.6 => /lib/x86_64-linux-gnu/libXext.so.6 (0x00007f5cbd61a000)
libpixman-1.so.0 => /lib/x86_64-linux-gnu/libpixman-1.so.0 (0x00007f5cbd573000)
libpng16.so.16 => /lib/x86_64-linux-gnu/libpng16.so.16 (0x00007f5cbd539000)
libxcb-shm.so.0 => /lib/x86_64-linux-gnu/libxcb-shm.so.0 (0x00007f5cbd534000)
libxcb.so.1 => /lib/x86_64-linux-gnu/libxcb.so.1 (0x00007f5cbd50a000)
libxcb-render.so.0 => /lib/x86_64-linux-gnu/libxcb-render.so.0 (0x00007f5cbd4fb000)
libXrender.so.1 => /lib/x86_64-linux-gnu/libXrender.so.1 (0x00007f5cbd2f1000)
libffi.so.7 => /lib/x86_64-linux-gnu/libffi.so.7 (0x00007f5cbd2e5000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f5cbd270000)
libjpeg.so.8 => /lib/x86_64-linux-gnu/libjpeg.so.8 (0x00007f5cbd1eb000)
libtiff.so.5 => /lib/x86_64-linux-gnu/libtiff.so.5 (0x00007f5cbd16a000)
libIlmImf-2_3.so.24 => /lib/x86_64-linux-gnu/libIlmImf-2_3.so.24 (0x00007f5cbce8c000)
libquadmath.so.0 => /lib/x86_64-linux-gnu/libquadmath.so.0 (0x00007f5cbce42000)
libdbus-1.so.3 => /lib/x86_64-linux-gnu/libdbus-1.so.3 (0x00007f5cbcdef000)
libatspi.so.0 => /lib/x86_64-linux-gnu/libatspi.so.0 (0x00007f5cbcdb8000)
libmount.so.1 => /lib/x86_64-linux-gnu/libmount.so.1 (0x00007f5cbcd58000)
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f5cbcd2d000)
libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007f5cbcd11000)
libthai.so.0 => /lib/x86_64-linux-gnu/libthai.so.0 (0x00007f5cbcd04000)
libgraphite2.so.3 => /lib/x86_64-linux-gnu/libgraphite2.so.3 (0x00007f5cbccd7000)
libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f5cbcca9000)
libuuid.so.1 => /lib/x86_64-linux-gnu/libuuid.so.1 (0x00007f5cbcca0000)
libXau.so.6 => /lib/x86_64-linux-gnu/libXau.so.6 (0x00007f5cbcc9a000)
libXdmcp.so.6 => /lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007f5cbcc90000)
libwebp.so.6 => /lib/x86_64-linux-gnu/libwebp.so.6 (0x00007f5cbca27000)
libzstd.so.1 => /lib/x86_64-linux-gnu/libzstd.so.1 (0x00007f5cbc97e000)
liblzma.so.5 => /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007f5cbc955000)
libjbig.so.0 => /lib/x86_64-linux-gnu/libjbig.so.0 (0x00007f5cbc747000)
libHalf.so.24 => /lib/x86_64-linux-gnu/libHalf.so.24 (0x00007f5cbc700000)
libIex-2_3.so.24 => /lib/x86_64-linux-gnu/libIex-2_3.so.24 (0x00007f5cbc6df000)
libIlmThread-2_3.so.24 => /lib/x86_64-linux-gnu/libIlmThread-2_3.so.24 (0x00007f5cbc6d5000)
libsystemd.so.0 => /lib/x86_64-linux-gnu/libsystemd.so.0 (0x00007f5cbc628000)
libblkid.so.1 => /lib/x86_64-linux-gnu/libblkid.so.1 (0x00007f5cbc5d1000)
libpcre2-8.so.0 => /lib/x86_64-linux-gnu/libpcre2-8.so.0 (0x00007f5cbc53f000)
libdatrie.so.1 => /lib/x86_64-linux-gnu/libdatrie.so.1 (0x00007f5cbc535000)
libbsd.so.0 => /lib/x86_64-linux-gnu/libbsd.so.0 (0x00007f5cbc51b000)
liblz4.so.1 => /lib/x86_64-linux-gnu/liblz4.so.1 (0x00007f5cbc4fa000)
libgcrypt.so.20 => /lib/x86_64-linux-gnu/libgcrypt.so.20 (0x00007f5cbc3dc000)
libgpg-error.so.0 => /lib/x86_64-linux-gnu/libgpg-error.so.0 (0x00007f5cbc3b7000)
which is fine and theres no problem. However the lib file in the install dir has a completely different MD5 hash and ldd output:
ldd output:
linux-vdso.so.1 (0x00007ffcec9c5000)
libtorch.so => not found
libBlinker.so => not found
libtorch_cpu.so => not found
libopencv_core.so.3.4 => /usr/local/lib/libopencv_core.so.3.4 (0x00007fe00f5f5000)
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fe00f414000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fe00f3f9000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe00f205000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fe00f1ff000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fe00f1dc000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fe00f1c0000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fe00f071000)
/lib64/ld-linux-x86-64.so.2 (0x00007fe010624000)
What is wrong here? is this a CMake bug? or is it related to OS (Ubuntu 20.04) or neither?
Solution
This is normal behavior in CMake. When compiling you binaries the rpath to the dependencies is set in the binary, but stripped when installing it.
By default if you don't change any RPATH related settings, CMake will link the executables and shared libraries with full RPATH to all used libraries in the build tree. When installing, it will clear the RPATH of these targets so they are installed with an empty RPATH. Source
When looking up runtime dependencies the rpath is preferred over the default directories (see https://stackoverflow.com/a/33520976/4181011). But since the rpath was removed from your library it doesn't know about you "out-of-default-directories"-dependencies.
You can manipulate the lookup with LD_LIBRARY_PATH
, LD_PRELOAD
or by adding the additional path to the lookup directories using ldconfig
.
Answered By - Simon Kraemer