Search code examples
c++opencvmakefilecmakecc

OpenCV library error when compiling with CMake and Make: cannot find -lopencv_bgsegm


I was trying to build a repo located HERE. As per instructions, I created a CMakeLists, and updated the directory addresses inside. I was not sure about the OpenCV path, but found two candidates and inserted both.

Cmake is good, but when doing make, I get this error, which I guess is maybe due to the openCV path being incorrect.

Here is the VERBOSE output:

[100%] Linking CXX executable SimpleVideoSummExample
/usr/bin/cmake -E cmake_link_script CMakeFiles/SimpleVideoSummExample.dir/link.txt --verbose=1
/usr/bin/c++    -O3 -std=c++0x -std=c++0x -fopenmp  -s CMakeFiles/SimpleVideoSummExample.dir/src/utils/ShotDetector.cc.o CMakeFiles/SimpleVideoSummExample.dir/src/videoSummarization/SimpleVideoSummarizer.cc.o CMakeFiles/SimpleVideoSummExample.dir/src/utils/ColorUtils.cc.o CMakeFiles/SimpleVideoSummExample.dir/src/utils/ImageUtils.cc.o CMakeFiles/SimpleVideoSummExample.dir/examples/SimpleVideoSummExample.cc.o  -o SimpleVideoSummExample  -L/usr/local/Cellar/opencv/3.4.1_2/lib -rdynamic libsummengine.a -lopencv_imgproc -lopencv_core -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_bgsegm -lopencv_video -lopencv_objdetect -lopencv_dnn -Wl,-rpath,/usr/local/Cellar/opencv/3.4.1_2/lib
/usr/bin/ld: cannot find -lopencv_bgsegm
collect2: error: ld returned 1 exit status
CMakeFiles/SimpleVideoSummExample.dir/build.make:199: recipe for target 'SimpleVideoSummExample' failed
make[2]: *** [SimpleVideoSummExample] Error 1

What is the problem? How to fix it?

Here is the directory part of CMakeLists (Cellar is the git repo's user):

include_directories(/usr/local/Cellar/opencv/3.4.1_2/include/ /usr/local/Cellar/opencv/3.4.1_2/include/opencv/ /usr/include/opencv /usr/local/include/opencv$
link_directories(/usr/local/Cellar/opencv/3.4.1_2/lib/)
add_executable(SimpleVideoSummExample src/utils/ShotDetector.cc src/videoSummarization/SimpleVideoSummarizer.cc src/utils/ColorUtils.cc src/utils/ImageUtils$
target_link_libraries(SimpleVideoSummExample
    summengine -lopencv_imgproc
    -lopencv_core
    -lopencv_highgui
    -lopencv_videoio
    -lopencv_imgcodecs
    -lopencv_bgsegm
    -lopencv_video
    -lopencv_objdetect
    -lopencv_dnn
)

Solution

  • Although the Github repository you are using doesn't explicitly state it in the documentation, it appears you not only need OpenCV, but also need to build the extra OpenCV modules (hinted by this answer). The missing opencv_bgsegm library is provided by these "extra" OpenCV modules.

    Following a tutorial such as this, you should include the CMake definition flag:

    -DOPENCV_EXTRA_MODULES_PATH=/path/to/opencv_contrib/modules/

    when running cmake. This will ensure the extra modules are built, and the opencv_bgsegm library will be available on your system.