不配置OpenCV环境,以动态链接的方式使用OpenCV


很多时候我们并不想配置OpenCV环境(可能是由于条件不允许,比如没有服务器的root权限,或者已经配置了不同版本的OpenCV)我们只需要动态链接的方式使用openCV.

上一篇博客提到了我要用一个依赖Cuda版本编译的Opencv2.4.9的C++ 程序(GPU提取视频光流特征)。

程序中原来的CMakeLists.txt 是这样链接到OpenCV 的:

# OpenCV Config
find_package( OpenCV REQUIRED )

message(STATUS "    version: ${OpenCV_VERSION}")
message("-- OPENCV include:   " ${OpenCV_INCLUDE_DIRS})
message("-- OPENCV libs dir:  " ${OpenCV_LIB_DIR})
message("-- OPENCV libs:   " ${OpenCV_LIBS} )

INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
LINK_DIRECTORIES(${OpenCV_LIB_DIR})

TARGET_LINK_LIBRARIES(
  compute_flow
  ${OPENCV_LIBRARIES}
  ${OpenCV_LIBS}
${Qt5Widgets_LIBRARIES}
)

因为服务器上默认的OpenCV 版本是3.2 ,find_package( OpenCV REQUIRED )找的的是OpenCV3.2的路径,因此Cmake 会报错(OpenCV3 相比OpenCv2在头文件位置,函数API调用方面有比较大的改动,可以说OpenCV3没有兼容OpenCV2)

我想让我的程序链接OpenCV 2.4

上篇博客讲了,我编译的OpenCV 2.4 在  目录home/lixinhui/local/opencv2下.有bin ,include ,lib ,share 四个文件夹,我们主要使用include (头文件) 和 lib(动态库文件libxxx.so)文件夹.


因此我需要做如下改动

# OpenCV Config
#find_package( OpenCV REQUIRED )

#message(STATUS "    version: ${OpenCV_VERSION}")
#message("-- OPENCV include:   " ${OpenCV_INCLUDE_DIRS})
#message("-- OPENCV libs dir:  " ${OpenCV_LIB_DIR})
#message("-- OPENCV libs:   " ${OpenCV_LIBS} )

set(OpenCV_INCLUDE_DIRS /home/lixinhui/local/opencv2/include)
set(OpenCV_LIB_DIR /home/lixinhui/local/opencv2/lib)

set(OpenCV_LIBS /home/lixinhui/local/opencv2/lib/libopencv_core.so /home/lixinhui/local/opencv2/lib/libopencv_gpu.so /home/lixinhui/local/opencv2/lib/libopencv_highgui.so /home/lixinhui/local/opencv2/lib/libopencv_imgproc.so /home/lixinhui/local/opencv2/lib/libopencv_video.so /home/lixinhui/local/opencv2/lib/libopencv_gpu.so /usr/lib/x86_64-linux-gnu/libtiff.so)


TARGET_LINK_LIBRARIES(
  compute_flow
  ${OPENCV_LIBRARIES}
  ${OpenCV_LIBS}
${Qt5Widgets_LIBRARIES}
)

也就是说我手动设置了OpenCV头文件路径,库文件路径,和用到的一系列动态库文件。这样程序就可以链接到Opencv 2.4 ,不用设置OpenCV 2.4的环境变量,直接以动态链接的方式使用OpenCV。






你可能感兴趣的:(代码)