1 2 3 4

使用Cmake+vs2017编译带opencv的项目

使用Cmake+vs2017编译带opencv的项目

最近经常用到这个方法,每次都会有一些挫折(忘了操作步骤),特此记录一下,以便日后查询

1、新建项目文件夹

mkdir demo_project

cd demo_project

# 创建CMakeLists.txt,内容为下:
cmake_minimum_required(VERSION 3.15)
# 自定义项目名为demo
project(demo)
set(CMAKE_CXX_STANDARD 11)
# find opencv
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
# 编译目标cpp为demo.cpp
add_executable(demo demo.cpp)
# directory of opencv library
link_directories(${OpenCV_LIBRARY_DIRS})
# opencv libraries
target_link_libraries(demo ${OpenCV_LIBS})

2、build

创建build构建文件夹,防止生成的文件泛滥

mkdir build
cd build
cmake -G"Visual Studio 15 2017 Win64" ^
	  -DOpenCV_DIR=C:\\"Program Files"\\opencv\\build\\x64\\vc15\\lib ..

其中-DOpenCV_DIR为包含了OpenCVConfig.cmake的目录(^在实际中是不存在的,表示连接)

以下为我的成功log

C:\Users\Breeze\Desktop\cv_class\assignment5\build>cmake -G"Visual Studio 15 2017 Win64" -DOpenCV_DIR=C:\\"Program Files"\\opencv\\build\\x64\\vc15\\lib ..
-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.18362.
-- The C compiler identification is MSVC 19.16.27045.0
-- The CXX compiler identification is MSVC 19.16.27045.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: C:/Program Files/opencv/build (found version "3.4.9")
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Breeze/Desktop/cv_class/assignment5/build

3、vs2017

在build文件夹中直接点开sln文件,就可以进入vs,注意选择匹配的X64或X86,有一次用cmd32来cmake搞了半天在vs打开也总是win32的配置

也就是说cmake的功能就是很方便的帮助了我们完成原本十分繁琐的vs项目配置

4、其他

用vs打开以后就可以编译运行了

但可能会有ALL_BUILD和ZERO_CHECK又会来骚扰你,不过可以不用管他,编译没报错就ok,可以在命令行来继续运行(虽然有点繁琐)。解决办法就是在VS的解决方案里右击项目,选择设为启动项目,不用管解决方案里的“ALL_BUILD”和“ZERO_CHECK”,或者可以直接卸载这两个项目

一般由CMAKE构建的解决方案(Solution)中包含三工程(Project),分别是ALL_BUILD、ZERO_CHECK、INSTALL,其中:

ALL_BUILD只要编译这个工程,所有的工程均会编译;

ZERO_CHECK监视CMakeLists.txt文件的变化,一旦发生变化,它会告诉编译器重新构建整个工程环境;

INSTALL:将工程编译后生成的dll和exe等安装到指定目录中,具体安装位置和安装内容详见该工程的Build Event->Post-Build Event->Command Line。

posted @ 2021-04-27 23:05  Breezerf  阅读(596)  评论(0编辑  收藏  举报