环境:gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
我使用find_package( OpenCV REQUIRED )
找到opencv库,使用target_link_libraries(executable ${OpenCV_LIBS})
链接opencv库。
我打开SET(CMAKE_VERBOSE_MAKEFILE 1)
,这样我就可以看到编译器的命令。
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})
从编译器输出(链接)中可以看到,opencv库在-ldl -lm -lpthread -lrt -lGLU -lGL -lSM -lICE -lX11 -lXext -ltbb
之后被链接了两次。当-lrt
需要放在编译器命令的末尾时,这似乎会产生问题,如下所示:link
如果我在编译器命令的末尾手动添加-lrt
(即-lrt
出现两次,一次在opencv库之前,一次在之后),它就不起作用了。
问题:
为什么opencv链接两次?
我应该如何克服这个问题,使
-lrt
被放在编译器命令的末尾只有一次?
MESSAGE("${OpenCV_LIBS}")
给我:opencv_videostab; opencv_video ; opencv_ts; opencv_superres; opencv_stitching; opencv_photo; opencv_ocl; opencv_objdetect; opencv_nonfree;开放ncv_ml; opencv_legacy opencv_imgproc; opencv_highgui; opencv_gpu opencv_flann; opencv_features2d; opencv_core; opencv_contrib; opencv_calib3 d
而的编译命令是:/usr/local/lib/libopencv_videostab.so.2.4.9 /usr/local/lib/libopencv_ts。/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轻轨车-lGLU -lGL lsm虱子-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/地方/lib/libopencv_video.so.2.4.9 2.4.9/usr/local/lib/libopencv_features2d.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
可以看到,在${OpenCV_LIBS}
中,opencv_video模块列在opencv_ts模块之前,而在编译器命令中,它被放在后面。我想知道这是否是一些opencv的自我依赖问题(例如,opencv_video依赖于opencv_videostab,应该先链接?
似乎CMake没有发货的文件FindOpenCV.cmake
,所以你使用3d一方来配置OpenCV库。
- 为什么opencv链接两次?
可能,FindOpenCV.cmake
的实现并不完全正确。
- 我应该如何克服这个问题,使-lrt被放在编译器命令的末尾只有一次?
把OpenCV_LIBS
修改成你想要的,然后再链接到它:
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