Linux下cmake 编译opencv demo 2种方法及其注意事项

首先上demo主程序代码,另存位opencv_demo.cpp

#include 
#include 

using namespace std;
using namespace cv;

int main(void)
{
  Mat src_image_ = imread("test.jpg");   // 读取当前目录下的test.jpg图片
  imshow("src_image_", src_image_);   // 打开图片文件
  waitKey(5000);     // 停留5秒钟
  return 0;
}

cmake编译

首先确保已经安装了cmake,然后建立一个CMakeLists.txt文件,输入如下内容

cmake_minimum_required(VERSION 2.8)
project(opencv)
find_package(OpenCV REQUIRED)
add_executable(opencv opencv.cpp)
target_link_libraries(opencv ${OpenCV_LIBS})

project()括号里面填写要生成的文件名称,add_executable()及其后一行类似,然后可以使用cmake命令运行,此次执行

cmake .
make
./opencv

如果运行没有问题则直接跳过,我遇到的问题如下:

CMake Warning at /usr/local/opencv-2.4.9/cmake/OpenCVConfig.cmake:161 (message):
  Found OpenCV Windows Pack but it has not binaries compatible with your
  configuration.

  You should manually point CMake variable OpenCV_DIR to your build of OpenCV
  library.
Call Stack (most recent call first):
  CMakeLists.txt:3 (find_package)


CMake Error at CMakeLists.txt:3 (find_package):
  Found package configuration file:

    /usr/local/opencv-2.4.9/cmake/OpenCVConfig.cmake

  but it set OpenCV_FOUND to FALSE so package "OpenCV" is considered to be
  NOT FOUND.


-- Configuring incomplete, errors occurred!

主要原因是我电脑上装了opencv3和opencv2

find_package这个指令以被用来在系统中自动查找配置构建工程所需的程序库。在linux和unix类系统下这个命令尤其有用。CMake自带的模块文件里有大半是对各种常见开源库的find_package支持,支持库的种类非常多。
当它找到OpenCV程序库之后,就会帮助我们预定义如下几个变量:
OpenCV_FOUND:是否找到OpenCV
OpenCV_INCLUDE_DIRS:OpenCV的头文件目录
OpenCV_LIBRARY_DIRS:OpenCV的库文件目录
OpenCV_LIBRARIES:OpenCV的所有库文件列表

解决方法的思路就是想办法绕过opencv2,所以整理了下面两种方法
方法1:
手工设置变量参数进行编译,主要设置如下几个参数:
OpenCV_INCLUDE_DIRS:需要包含的OpenCV的头文件目录
OpenCV_LIBRARY_DIRS:需要包含的OpenCV的库文件目录
OpenCV_LIBS:OpenCV的所有库文件列表,这个参数与我上一篇博客讲的一样,可以通过pkg-config opencv --libs 来获取静态库文件名称,上个设置的库目录下的.so文件,以-l(小写的字母L)后面直接跟省略了尾缀(".so")和头缀(“lib”)的文件名。
将CMakeLists.txt文件内容更新为下面代码:

cmake_minimum_required(VERSION 2.8)
project(demo3)
#find_package(OpenCV REQUIRED)
#if(Opencv_FOUND)
#	message(STATUS "The Opencv lib is found!")
set(OpenCV_INCLUDE_DIRS "/usr/local/include/opencv /usr/local/include")
set(OpenCV_LIBRARY_DIRS "/usr/local/lib")
set(OpenCV_LIBS "-lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core")
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(demo3 opencv_demo.cpp)
link_directories(${OpenCV_LIBRARY_DIRS})
target_link_libraries(demo3 ${OpenCV_LIBS})

方法2:
直面问题,找到opencv3的OpenCVConfig.cmake,然后设置上,再利用find_package(OpenCV REQUIRED)命令。
找OpenCVConfig.cmake命令:locate OpenCVConfig.cmake,运行后弹出下面信息

zhupenghui@zhupenghui-virtual-machine:~/MyWork/Demo/test_opencv2$ locate OpenCVConfig.cmake
/home/zhupenghui/opencv/opencv-3.2.0/build/OpenCVConfig.cmake
/home/zhupenghui/opencv/opencv-3.2.0/build/unix-install/OpenCVConfig.cmake
/home/zhupenghui/opencv/opencv-3.2.0/cmake/templates/OpenCVConfig.cmake.in
/usr/local/opencv-2.4.9/cmake/OpenCVConfig.cmake
/usr/local/opencv-2.4.9/cmake/templates/OpenCVConfig.cmake.in
/usr/local/opencv-2.4.9/release/OpenCVConfig.cmake
/usr/local/opencv-2.4.9/release/unix-install/OpenCVConfig.cmake
/usr/local/share/OpenCV/OpenCVConfig.cmake
/usr/share/OpenCV/OpenCVConfig.cmake

然后选择opencv3的build目录进行设置OpenCV_DIR变量(这里需要注意一下设置了如果是xxx_DIR变量,则在find_package(xxx REQUIRED),以后对应的变量也是xxx_INCLUDE_DIRS、xxx_LIBRARY_DIRS、xxx_LIBS,注意大小写,否则不一致设置了不能生效)
将CMakeLists.txt文件内容更新为下面代码:

cmake_minimum_required(VERSION 2.8)
project(demo3)
set(OpenCV_DIR /home/zhupenghui/opencv/opencv-3.2.0/build)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(demo3 opencv_demo.cpp)
link_directories(${OpenCV_LIBRARY_DIRS})
target_link_libraries(demo3 ${OpenCV_LIBS})

下面参考链接中利用了最后一个解决了问题,但是前面两个作参考,学了很多cmake opencv 的基础知识,再次感谢下面博文作者。

参考链接:
使用CMake构建OpenCV项目
深入理解CMake(3):find_package()的使用
听说你安装测试 OpenCV 总是不成功?你可能遇到这个find_package坑了!

你可能感兴趣的:(opencv,C++,g++)