cmake找不到opencv解决

目录

CMakeLists.txx中设置opencv路径

环境变量方法,ok

opencv cmake 编译入门示例


报错代码:

find_package(OpenCV REQUIRED)

CMakeLists.txx中设置opencv路径

解决方法1:主要问题在于库路径问题,应该在CMakeLists.txx中设置opencv路径指向build文件夹。

linux系统:

  1. set (OpenCV_DIR /path/to/opencv/build)

  2. find_package(OpenCV REQUIRED)

windows系统:测试ok

set (OpenCV_DIR E:/opencv/opencv)
#opencv
find_package(OpenCV REQUIRED)

也可以参考这个: 

https://blog.csdn.net/jacke121/article/details/110938303

环境变量方法,ok

配一个环境变量:OPENCV_DIR 到bin目录的父目录

set(OPENCV_DIR_HINT "")

if(WIN32)

    get_filename_component(OPENCV_DIR_PLATFORM $ENV{OPENCV_DIR} DIRECTORY)

    get_filename_component(OPENCV_DIR_HINT ${OPENCV_DIR_PLATFORM} DIRECTORY)

endif(WIN32)


set(OpenCV_STATIC OFF)

find_package(OpenCV REQUIRED HINTS ${OPENCV_DIR_HINT})

opencv cmake 编译入门示例

转自:听说你安装测试 OpenCV 总是不成功?你可能遇到这个find_package坑了! - 知乎

#include 
#include 
using namespace cv;
int main(int argc, char** argv ) {
  if ( argc != 2 ) {
      printf("usage: DisplayImage.out \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)
 if(Opencv_FOUND)
     message(STATUS "The Opecv lib is found!") 
 endif()
 add_executable( Display test.cpp)
 arget_link_libraries( Display ${OpenCV_LIBS} )

你可能感兴趣的:(opencv,opencv,计算机视觉,人工智能)