Configuring an c++ OpenCV project with Cmake

2019-01-11 22:15发布

问题:

I consider this a fundamental step for creating projects that use OpenCV libraries so you don't need to manually include all the libraries. There is not detailed information on this topic, at least for a newbie that just wants to use OpenCV as soon as posible, so:

Which is the easiest and scalable way to create a multiplatform c++ OpenCV with Cmake?

回答1:

First: create a folder Project containing two subfolders src and include, and a file called CMakeLists.txt.

Second: Put your cpp inside the src folder and your headers in the include folders.

Third: Your CMakeLists.txt should look like this:

cmake_minimum_required(VERSION 2.8) 
PROJECT (name)
find_package(OpenCV REQUIRED )
set( NAME_SRC
    src/main.cpp    
)

set( NAME_HEADERS       
     include/header.h
)

INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
link_directories( ${CMAKE_BINARY_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable( name ${NAME_SRC} ${NAME_HEADERS} )

target_link_libraries( sample_pcTest ${OpenCV_LIBS} )

Fourth: Open CMake GUI and select the root folder as input and create a build folder for the output. Click configure, then generate, and choose the generator (VisualStudio, Eclipse, ...)



回答2:

I am using opencv3.0 and cmake3.8, config below work for me!

######## A simple cmakelists.txt file for OpenCV() #############  
cmake_minimum_required(VERSION 2.8)                          # 初始化Cmake版本检测  
PROJECT(word)                                       # 工程名  

FIND_PACKAGE( OpenCV REQUIRED )                              # 环境变量中寻找OpenCV的库的位置  
INCLUDE_DIRECTORIES( ${OpenCV_INCLUDE_DIRS} )

ADD_EXECUTABLE(word main.c)                         # 将文件加入工程,有多少.c或者cpp都加进去  
TARGET_LINK_LIBRARIES (word ${OpenCV_LIBS})         # 这两行的次序也不能变!加入动态链接库  
########### end ####################################  


标签: opencv cmake