1

I'm trying to compile my C++ program which uses opencv, here is my code:

#include <iostream>

#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

int main()
{
    cout << "Hello world!\n";
    Mat image = cv::imread(R"(1596254171-image.jpg)");
    return 0;
}

It's a quite simple code. And here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

project(testbed_ VERSION 1.0)
set(EXECUTABLE_OUTPUT_PATH ../bin)

set(ext_third_party_dir /home/mtd/third_party)

set(opencv_lib_dir /home/mtd/third_party/opencv_4.2.0/lib/)
set(opencv_lib ${opencv_lib_dir}/libopencv_imgcodecs.so 
               ${opencv_lib_dir}/libopencv_imgproc.so.4.2
               ${opencv_lib_dir}/libopencv_core.so)

add_executable(testbed_ ../main.cpp)
target_include_directories(testbed_ PUBLIC ${ext_third_party_dir}/include)
target_link_libraries(testbed_ PUBLIC  ${opencv_lib} )

The compilation operation is everything OK, but when I executed my testbed_, I got the error:

error while loading shared libraries: libopencv_imgproc.so.4.2: cannot open shared object file: No such file or directory

I wonder why I've already specified the link path but the linker still cannot fint the imgproc lib, and why core.so can be found but imgproc.so not ?

Mactarvish
  • 65
  • 6
  • "I've already specified the link path but the linker still cannot fint the imgproc lib" - The error is emitted by the **loader**, not by the **linker**. CMake sets RPATH for your executable in a way, so every needed library can be loaded by the **executable**. But full-filling **inter-libraries** dependencies is your task. Run `ldd` for every OpenCV library you use and check that it finds all dependent libraries. See also that question: https://stackoverflow.com/questions/59638072/integrate-opencv-using-cmake-cannot-open-shared-object-file. – Tsyvarev May 09 '21 at 08:52
  • Thanks for your help. I've found that imgproc is needed by imgcodecs, but how can I specify "where does imgcodecs search for imgproc" ? – Mactarvish May 10 '21 at 14:53
  • Normally, a shared library on Linux contain paths to all needed libraries in form of RPATH. Your OpenCV libraries probably have these settings, but it seems you have **moved** these libraries, so RPATH no longer points to them. You could patch you libraries with proper RPATH. For that, see that question: https://stackoverflow.com/questions/13769141/can-i-change-rpath-in-an-already-compiled-binary. Alternatively, you add directory with your libraries to `LD_LIBRARY_PATH` environment variable: https://stackoverflow.com/questions/13428910. – Tsyvarev May 10 '21 at 15:16

0 Answers0