c – CMake找不到OpenCV库

与此question类似,我在OS X 10.10.3上使用OpenCV和CMake时遇到了麻烦.

经过一番麻烦,我终于设法在我的系统上构建OpenCV 3.0 beta;标题现在位于/usr/local/include中,而libs – 正如它们应该 – 位于/usr/local/lib中.我有小程序使用OpenCV,我的CMakeLists.txt看起来像这样

set( CMAKE_CXX_FLAGS         "-O3 -w" ) 
find_package( OpenCV REQUIRED )
if(OpenCV_FOUND)
   message("Found OpenCV")
   message("Includes: " ${OpenCV_INCLUDE_DIRS})
endif(OpenCV_FOUND)

add_executable( cannyDetector canny/canny.cpp )

target_link_libraries( cannyDetector ${OpenCV_LIBS} )

我很平常

Undefined symbols for architecture x86_64:
  "vtable for cv::_InputArray", referenced from:
      _main in correctImage.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for cv::_OutputArray", referenced from:
      _main in correctImage.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[2]: *** [correctImage] Error 1
make[1]: *** [CMakeFiles/correctImage.dir/all] Error 2
make: *** [all] Error 2

变量OpenCV_LIBS包含

opencv_xphoto;opencv_xobjdetect;opencv_ximgproc;opencv_xfeatures2d;opencv_tracking;opencv_text;opencv_surface_matching;opencv_saliency;opencv_rgbd;opencv_reg;opencv_optflow;opencv_line_descriptor;opencv_latentsvm;opencv_ccalib;opencv_bioinspired;opencv_bgsegm;opencv_adas;opencv_videostab;opencv_videoio;opencv_video;opencv_ts;opencv_superres;opencv_stitching;opencv_shape;opencv_photo;opencv_objdetect;opencv_ml;opencv_imgproc;opencv_imgcodecs;opencv_highgui;opencv_hal;opencv_flann;opencv_features2d;opencv_core;opencv_calib3d

此外,手动编译工作:

gcc -o canny $(pkg-config --cflags opencv3)  $(pkg-config --libs opencv3) -lstdc++ -w -O3  ../canny/canny.cpp

(我不知道为什么我必须指定-lstdc,但没有它,链接失败的原因是另一个原因).
运行make VERBOSE = 1时,将对可执行文件运行以下命令:

/opt/local/bin/g++   -O3 -w -lstdc++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/cannyDetector.dir/canny/canny.o  -o cannyDetector  /usr/local/lib/libopencv_xphoto.3.0.0.dylib /usr/local/lib/libopencv_ximgproc.3.0.0.dylib /usr/local/lib/libopencv_tracking.3.0.0.dylib /usr/local/lib/libopencv_text.3.0.0.dylib /usr/local/lib/libopencv_surface_matching.3.0.0.dylib /usr/local/lib/libopencv_saliency.3.0.0.dylib /usr/local/lib/libopencv_rgbd.3.0.0.dylib /usr/local/lib/libopencv_reg.3.0.0.dylib /usr/local/lib/libopencv_optflow.3.0.0.dylib /usr/local/lib/libopencv_line_descriptor.3.0.0.dylib /usr/local/lib/libopencv_latentsvm.3.0.0.dylib /usr/local/lib/libopencv_ccalib.3.0.0.dylib /usr/local/lib/libopencv_bioinspired.3.0.0.dylib /usr/local/lib/libopencv_bgsegm.3.0.0.dylib /usr/local/lib/libopencv_adas.3.0.0.dylib /usr/local/lib/libopencv_videostab.3.0.0.dylib /usr/local/lib/libopencv_ts.a /usr/local/lib/libopencv_superres.3.0.0.dylib /usr/local/lib/libopencv_stitching.3.0.0.dylib /usr/local/lib/libopencv_photo.3.0.0.dylib /usr/local/lib/libopencv_objdetect.3.0.0.dylib /usr/local/lib/libopencv_hal.a /usr/local/lib/libopencv_xobjdetect.3.0.0.dylib /usr/local/lib/libopencv_xfeatures2d.3.0.0.dylib /usr/local/lib/libopencv_shape.3.0.0.dylib /usr/local/lib/libopencv_video.3.0.0.dylib /usr/local/lib/libopencv_calib3d.3.0.0.dylib /usr/local/lib/libopencv_features2d.3.0.0.dylib /usr/local/lib/libopencv_ml.3.0.0.dylib /usr/local/lib/libopencv_highgui.3.0.0.dylib /usr/local/lib/libopencv_videoio.3.0.0.dylib /usr/local/lib/libopencv_imgcodecs.3.0.0.dylib /usr/local/lib/libopencv_imgproc.3.0.0.dylib /usr/local/lib/libopencv_flann.3.0.0.dylib /usr/local/lib/libopencv_core.3.0.0.dylib /usr/local/lib/libopencv_hal.a -framework AGL -framework OpenGL /usr/local/share/OpenCV/3rdparty/lib/libippicv.a

但是,如果我接受命令并用输入源文件../canny/canny.cpp替换输入文件CMakeFiles / cannyDetector.dir / canny / canny.o,它就可以了!

因此库都在那里,但它仍然无法编译,因为编译器无法链接目标文件,但编译和链接在一个步骤中工作.

任何人都可以建议这里发生了什么?

最佳答案
在我的情况下修复是指示CMake在哪里寻找OpenCV配置模块.

find_package(OpenCV REQUIRED PATHS /usr/local/share/OpenCV NO_DEFAULT_PATH) # manual specification of the OpenCVConfig.cmake path is necessary since otherwise, cmake assigns /opt/local a higher priority and ocv2.4 will be used

无论出于何种原因,/ opt / local / share的优先级高于/usr/local/share,因此只找到OpenCV 2.4的配置文件,但我使用的是3.0.我不确定是什么原因导致链接错误,但可能有些代码从库中移动到3.0版中的头文件,必须与用户代码一起编译,但不是因为CMake只包含版本2头文件.

include_directories(${OpenCV_INCLUDE_DIRS})

失踪.

转载注明原文:c – CMake找不到OpenCV库 - 代码日志