环境: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})
从编译器输出(link)可以看出,opencv库在-ldl -lm -lpthread -lrt -lGLU -lGL -lSM -lICE -lX11 -lXext -ltbb
之后链接了两次。
当-lrt
需要放在编译器命令的末尾时,这似乎会产生问题,如下所述:link
如果我在编译器命令的末尾手动添加-lrt
(即-lrt
出现两次,一次出现在opencv lib之后,一次出现在opencv lib之后),它就无法工作。
问题:
为什么opencv被链接两次?
我应该如何克服这个问题,以便-lrt
仅在编译器命令的末尾放置一次?
更新:
MESSAGE("${OpenCV_LIBS}")
给了我:
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
而编译器命令是:/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
您可以看到opencv_video模块在${OpenCV_LIBS}
中的opencv_ts模块之前列出,而在编译器命令中则是之后放置的。
我想知道这是否是一些opencv自我依赖问题(例如opencv_video取决于应首先链接的opencv_videostab?
答案 0 :(得分:0)
似乎CMake没有发送文件FindOpenCV.cmake
,因此您使用3d party one来配置OpenCV库。
- 为什么opencv被链接两次?
醇>
可能OpenCV_LIBS
实施并不完全正确。
- 我应该如何克服这个问题,以便-lrt只放在编译器命令的末尾一次?
醇>
在链接之前,只需将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
修改为您想要的一个:
USER