(五)CMake与OpenCV

直接看CMakeLists.txt是怎么写的吧![1]

# cmake needs this line
cmake_minimum_required(VERSION 2.8)

# Define project name
project(opencvTest)

# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)

# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

# Add OpenCV headers location to your include paths
include_directories(${OpenCV_INCLUDE_DIRS})

# Declare the executable target built from your sources
add_executable(main main.cpp)

# Link your application with OpenCV libraries
target_link_libraries(main ${OpenCV_LIBS})

首先通过find_package语句找到CMake所需要的所有变量如:

  • OpenCV_VERSION
  • OpenCV_LIBS
  • OpenCV_INCLUDE_DIRS

接下来他和普通的头文件、库的引用就没有什么区别了:

  • 头文件支持:include_directories(${OpenCV_INCLUDE_DIRS})
  • 库文件连接:target_link_libraries(main ${OpenCV_LIBS})

参考文章,有不同版本的OpenCV的指定方法,其实就是将变量设置一下,仅此而已。


[1] https://www.cnblogs.com/tsvico/p/11877933.html

你可能感兴趣的:(编译工具,opencv,CMake)