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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值