Win7下使用Cmake维护基于OpenCV的vs2012工程


当更改OpenCV版本或是将VS工程迁移到其他机器环境上时手动进行配置费时费力,使用Cmake进行维护可以大大提高效率

下面以OpenCV2.4.9+VS2012+cmake-2.8.12.2为例演示


OpenCV安装:

从OpenCV官网下载OpenCV2.4.9,解压到所需目录,本文中为D:\Runtime\opencv

压缩包解压出两个文件夹,build为已经编译好的release版本,source为源码可以用于自己编译

编译源码可以参考zhouyelihua的博客,本文使用build中提供的已经编译好的版本


在系统环境变量里添加OpenCV_DIR 值为D:\Runtime\opencv\build (此路径下应存在OpenCVConfig.cmake)

在系统环境变量Path的最后加入;D:\Runtime\opencv\build\x86\vc11\bin (注意要用分号与前面的值分隔)

检查OpenCVConfig.cmake 中的OpenCV_INCLUDE_DIRS和OpenCV_LIB_DIR路径是否正确

注销或重启后环境变量生效,OpenCV安装结束

CmakeLists:

下面是一个样例工程的Cmake文档

工程目录如下:

build

--CMakeLists.txt

--helloExe

--main.cpp

--CMakeLists.txt

--helloLib

--hello.cpp

--hello.hpp

--CMakeLists.txt

顶层CMakeLists

解决方案名为HELLO 包含一个Lib工程和一个可执行工程

cmake_minimum_required(VERSION 2.6)
project(HELLO)#solution name

add_subdirectory(helloLib)
add_subdirectory(helloExe)

helloLib的CMakeLists

注意OpenCV的三行顺序不能更改

cmake_minimum_required(VERSION 2.6)
set(LIB_SRC hello.cpp)
add_definitions("-DLIBHELLO_BUILD")
add_library(libhello SHARED ${LIB_SRC})               #libhello is the name of project
set_target_properties(libhello PROPERTIES OUTPUT_NAME "hello")

#add opencv, order of the following lines should not be changed
FIND_PACKAGE(OpenCV REQUIRED )                  
INCLUDE_DIRECTORIES( ${OPENCV_INCLUDE_DIR} )
TARGET_LINK_LIBRARIES(libhello ${OpenCV_LIBS})

helloExe的CMakeLists

添加了对HelloLib的依赖

cmake_minimum_required(VERSION 2.6)
include_directories(${PROJECT_SOURCE_DIR}/helloLib)
set(APP_SRC main.cpp)
add_executable(hello ${APP_SRC})#hello is the name of project
target_link_libraries(hello libhello)
add_dependencies(hello libhello)


FIND_PACKAGE(OpenCV REQUIRED )
INCLUDE_DIRECTORIES( ${OPENCV_INCLUDE_DIR} )
TARGET_LINK_LIBRARIES(hello ${OpenCV_LIBS})

最后到build文件夹所在的路径,使用cmake build即可



你可能感兴趣的:(计算机视觉)