我克隆、编译并安装了 OpenCV 的主分支:
cmake .. -DCMAKE_INSTALL_PREFIX=/some/non-system-path \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DCMAKE_INSTALL_RPATH=/some/non-system-path
make
make install
通常的东西,除了我将它安装在用户路径中。为了测试它,我尝试运行以下“hello world”示例:
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
if ( argc != 2 )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
使用以下 CMakeLists.txt
:
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage main.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
请注意,这是来自 OpenCV 的精确示例。它失败并显示以下内容:
[ 50%] Building CXX object CMakeFiles/DisplayImage.dir/main.cpp.o
make[2]: *** No rule to make target 'opencv_calib3d-NOTFOUND', needed by 'DisplayImage'. Stop.
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/DisplayImage.dir/all' failed
make[1]: *** [CMakeFiles/DisplayImage.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
查看 /some/non-system-path/lib
存在 libopencv_calib3d.so
指向 libopencv_calib3d.so.3.3.1
.
我认为这可能是 link_directories
问题,但是,设置它也没有解决问题。至于 ${OpenCV_LIBS}
我打印了它并得到了:
message(STATUS "OpenCV_LIBS = ${OpenCV_LIBS}")
-- OpenCV_LIBS = opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab;opencv_viz
有什么想法吗?
最佳答案
我在使用 CMake 的 Visual Studio 2019 在 Windows 10 上构建 OpenCV 时遇到了这个问题。 x64-Debug
配置构建成功,但 x64-Release
有 opencv_calib3d-NOTFOUND
错误。
引自 alalek comment :
OpenCV supported builds are Debug and Release only. Other build configurations are not supported at all and proper compiler flags are not applied.
我注意到 CMakeSettings.json 的 RelWithDebInfo
配置类型为 x64-Release
。所以我将其更改为 Release
配置类型并解决了问题。
P. S. 还描述了可能的解决方案 here , 但我没有尝试。
关于c++ - 链接到从源代码构建的 OpenCV 时出现 Cmake 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47218840/