0
votes

Environment: gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4

I use find_package( OpenCV REQUIRED ) to find the opencv library and use target_link_libraries(executable ${OpenCV_LIBS}) to link the opencv.

I switch on SET(CMAKE_VERBOSE_MAKEFILE 1) so that I can see what are the commands for the compiler.

CMakeLists.txt:

cmake_minimum_required (VERSION 2.8.3)
project (video_capture)

find_package( OpenCV REQUIRED )

SET(CMAKE_VERBOSE_MAKEFILE 1)

add_executable( vidcapture main.cpp)

target_link_libraries(vidcapture ${OpenCV_LIBS})

As you can see from the compiler output (link), the opencv libraries are linked twice after the -ldl -lm -lpthread -lrt -lGLU -lGL -lSM -lICE -lX11 -lXext -ltbb. This seems creates issue when -lrt needs to be put at the end of the compiler commands as explained here: link

It didn't work if I manually add the -lrt at the end of the compiler commands (i.e. -lrt appear twice, one before the opencv lib and one after).

Question:

  1. Why the opencv is linked twice?

  2. How should I overcome this issue so that -lrt is put at the end of the compiler commands only once?

UPDATE:

MESSAGE("${OpenCV_LIBS}") gives me: opencv_videostab;opencv_video;opencv_ts;opencv_superres;opencv_stitching;opencv_photo;opencv_ocl;opencv_objdetect;opencv_nonfree;ope ncv_ml;opencv_legacy;opencv_imgproc;opencv_highgui;opencv_gpu;opencv_flann;opencv_features2d;opencv_core;opencv_contrib;opencv_calib 3d

while the compiler commands are:/usr/local/lib/libopencv_videostab.so.2.4.9 /usr/local/lib/libopencv_ts.a /usr/local/lib/libopencv_superres.so.2.4.9 /usr/local/lib/libopencv_stitching.so.2.4.9 /usr/local/lib/libopencv_contrib.so.2.4.9 -ldl -lm -lpthread -lrt -lGLU -lGL -lSM -lICE -lX11 -lXext -ltbb /usr/local/lib/libopencv_nonfree.so.2.4.9 /usr/local/lib/libopencv_ocl.so.2.4.9 /usr/local/lib/libopencv_gpu.so.2.4.9 /usr/local/lib/libopencv_photo.so.2.4.9 /usr/local/lib/libopencv_objdetect.so.2.4.9 /usr/local/lib/libopencv_legacy.so.2.4.9 /usr/local/lib/libopencv_video.so.2.4.9 /usr/local/lib/libopencv_ml.so.2.4.9 /usr/local/lib/libopencv_calib3d.so.2.4.9 /usr/local/lib/libopencv_features2d.so.2.4.9 /usr/local/lib/libopencv_highgui.so.2.4.9 /usr/local/lib/libopencv_imgproc.so.2.4.9 /usr/local/lib/libopencv_flann.so.2.4.9 /usr/local/lib/libopencv_core.so.2.4.9 -Wl,-rpath,/usr/local/lib

You can see that the opencv_video module is list before the opencv_ts module in the ${OpenCV_LIBS} while in the compiler command it is put at afterwards. I was wondering if this is some opencv self dependency issue (e.g. the opencv_video depends on the opencv_videostab which should be linked first?

1

1 Answers

0
votes

It seems that CMake has no shipped file FindOpenCV.cmake, so you use 3d party one for configure OpenCV library.

  1. Why the opencv is linked twice?

Probably, FindOpenCV.cmake implementation is not perfectly correct.

  1. How should I overcome this issue so that -lrt is put at the end of the compiler commands only once?

Just modify OpenCV_LIBS to one you want before linking to it:

list(REMOVE_DUPLICATES OpenCV_LIBS) # duplicates are not needed anywhere
list(REMOVE_ITEM OpenCV_LIBS "-lrt") # remove "rt" library from the list..
list(APPEND OpenCV_LIBS "-lrt") # ..and insert it to the end