CMake文件用法,以OpenCV为例

学习记录

代码

cmake_minimum_required (VERSION 2.6)

cmake_policy(SET CMP0012 NEW)

PROJECT(proexe)

FIND_PACKAGE( OpenCV 4.3.0 REQUIRED )
MESSAGE("OpenCV version : ${OpenCV_VERSION}--------------------->>>>>>>>>")

include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIB_DIR})

ADD_EXECUTABLE( ${PROJECT_NAME} main.cpp)
TARGET_LINK_LIBRARIES( ${PROJECT_NAME} ${OpenCV_LIBS} )

逐行分析

# 检查CMake版本,至少为2.6
# 如果CMake的当前版本低于指定的版本,它会停止处理工程文件,并报告错误。
cmake_minimum_required (VERSION 2.6)
# 指定策略,具体不了解
cmake_policy(SET CMP0012 NEW)
# 设置工程名为“proexe”
PROJECT(proexe)

# 使用find_package引入外部依赖包
FIND_PACKAGE( OpenCV 4.3.0 REQUIRED )
# 为用户显示一条消息。
MESSAGE("OpenCV version : ${OpenCV_VERSION}--------------------->>>>>>>>>")
# 将OpenCV_INCLUDE_DIRS目录下的文件加入包含目录列表
include_directories(${OpenCV_INCLUDE_DIRS})
# 将OpenCV_LIB_DIR加入编译器链接阶段的搜索目录列表
link_directories(${OpenCV_LIB_DIR})
# 使用main.cpp文件生成一个可执行文件,文件名为工程名
ADD_EXECUTABLE( ${PROJECT_NAME} main.cpp)
# 将OpenCV_LIBS路径下文件链接到可执行文件中
TARGET_LINK_LIBRARIES( ${PROJECT_NAME} ${OpenCV_LIBS} )

运行命令

root@raspberrypi:/home/pi/Code/detection# cd build/
root@raspberrypi:/home/pi/Code/detection/build# cmake ..
-- The C compiler identification is GNU 8.3.0
-- The CXX compiler identification is GNU 8.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: /usr/local (found suitable version "4.3.0", minimum required is "4.3.0") 
OpenCV version : 4.3.0--------------------->>>>>>>>>
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/Code/detection/build
root@raspberrypi:/home/pi/Code/detection/build# make
Scanning dependencies of target Project
[ 50%] Building CXX object CMakeFiles/Project.dir/main.cpp.o
[100%] Linking CXX executable Project
[100%] Built target Project
root@raspberrypi:/home/pi/Code/detection/build# ./Project

你可能感兴趣的:(OpenCV)