由于项目需求,需要跨平台编译Dlib+OpenCV,并借用MSVC编译器、Qt界面、CUDA加速。
经过摸索,成功实现跨平台编译。切入正题
参考文章:
https://www.cnblogs.com/JoyPoint/p/10549774.html,基于Cmake+QT+VS的C++项目构建开发编译简明教程
https://blog.csdn.net/u012234115/article/details/90642844?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task,机器学习库dlib的C++编译和使用(windows和linux)
————————————————————————
(1)Qt5.11.1, 使用MSVC2015_64编译器
(2)Visual Studio 2015 update3,安装“Qt Visual Studio Tools”、QtPackage扩展和更新
(3)Cmake-3.16.5-win64-x64
(4)CUDA-10.1,cudnn-10.1-windows7-x64-v7.6.5.32
(5)dlib-19.19.0
(6)opencv-4.1.2
(7)Anaconda3-4.2.0-Windows-x86_64(玩python时装的,cmake时会检索图片库)
安装此处不再解释,默认安装,把自己需要的模块安装上就好
(1)Qt系统路径,D:\Qt\Qt5.11.1\5.11.1\msvc2015_64\bin;D:\Qt\Qt5.11.1\5.11.1\msvc2015_64\include;D:\Qt\Qt5.11.1\5.11.1\msvc2015_64\lib;
(2)VS系统路径,C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64;
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include;
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin;
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\lib;
选择【QT VS Tools】-> 【Qt option】,添加Qt版本,名称“Qt5.11.1”,路径“D:\Qt\Qt5.11.1\5.11.1\msvc2015_64\”
(3)Cmake系统路径,D:\CMake;D:\CMake\bin;
并在系统变量中增加变量名:“CMAKE_PREFIX_PATH”,变量值:“D:\Qt\Qt5.11.1\5.11.1\msvc2015_64\lib\cmake”
(4)CUDA系统路径,C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin;
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\libnvvp;
C:\Program Files\NVIDIA Corporation\Nsight Compute 2019.3.0\;
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;
C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;
(5)dlib不需要配
(6)openCV系统路径,D:\OpenCV412\opencv\build\include;
D:\OpenCV412\opencv\build\x64\vc14\bin;
D:\OpenCV412\opencv\build\x64\vc14\lib;
D:\OpenCV412\opencv\build;
(7)Anaconda3系统路径,D:\Program Files (x86)\Anaconda3;
D:\Program Files (x86)\Anaconda3\Scripts;
D:\Program Files (x86)\Anaconda3\Library\bin;
Qt Widgets Application项目新建,选择MSVC2015 x64编译套件。
dlib-19.19.0目录下创建以“CMakeLists.txt”命名的文本文件,其内容如下:
cmake_minimum_required(VERSION 2.8.12)
#############################################################################
# #
# READ examples/CMakeLists.txt TO SEE HOW TO USE DLIB FROM C++ WITH CMAKE #
# #
#############################################################################
get_directory_property(has_parent PARENT_DIRECTORY)
if(NOT has_parent)
# When you call add_subdirectory(dlib) from a parent CMake project dlib's
# CMake scripts will assume you want to statically compile dlib into
# whatever you are building rather than create a standalone copy of dlib.
# This means CMake will build dlib as a static library, disable dlib's
# install targets so they don't clutter your project, and adjust a few other
# minor things that are convenient when statically building dlib as part of
# your own projects.
#
# On the other hand, if there is no parent CMake project or if
# DLIB_IN_PROJECT_BUILD is set to false, CMake will compile dlib as a normal
# standalone library (either shared or static, based on the state of CMake's
# BUILD_SHARED_LIBS flag), and include the usual install targets so you can
# install dlib on your computer via `make install`. Since the only reason
# to build this CMakeLists.txt (the one you are reading right now) by itself
# is if you want to install dlib, we indicate as such by setting
# DLIB_IN_PROJECT_BUILD to false.
set(DLIB_IN_PROJECT_BUILD false)
endif()
add_subdirectory(dlib)
在上述目录下新建一个以“CMakeLists.txt”命名的文本文件,用Notepad编辑即可,其内容如下:
#设置cmake版本号
cmake_minimum_required(VERSION 3.4.0)
#设置工程名称
project(QtGuiDemo)
#设置工程包含当前目录,非必须
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#打开全局moc,设置自动生成moc文件,一定要设置
set(CMAKE_AUTOMOC ON)
#打开全局uic,非必须
set(CMAKE_AUTOUIC ON)
#打开全局rcc,非必须,如需打开,注意修改33行的qrc文件名
#set(CMAKE_AUTORCC ON)
#查找需要的Qt库文件,最好每一个库都要写,Qt也会根据依赖关系自动添加
find_package(Qt5Widgets)
find_package(Qt5Core)
find_package(Qt5Gui)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS})
include(../../PythonAlgorithm/dlib-19.19.0/dlib/cmake)
#查找当前文件夹中的所有源代码文件,也可以通过Set命令将所有文件设置为一个变量
FILE(GLOB SRC_FILES "./*.cpp")
#查找设置当前文件夹中所有的头文件
FILE(GLOB HEAD_FILES "./*.h")
#查找设置当前文件夹中所有的ui文件
FILE(GLOB UI_FILES "./*.ui")
#通过Ui文件生成对应的头文件,一定要添加
qt5_wrap_ui(WRAP_FILES ${UI_FILES})
#添加资源文件,非必须,一旦采用,注意修改相应的qrc文件名
#set(RCC_FILES rcc.qrc)
#将ui文件和生成文件整理在一个文件夹中,非必须
source_group("Ui" FILES ${UI_FILES} ${WRAP_FILES} )
#创建工程文件
add_executable(${PROJECT_NAME} ${SRC_FILES} ${HEAD_FILES} ${RCC_FILES} ${WRAP_FILES})
#添加Qt5依赖项
target_link_libraries(${PROJECT_NAME} Qt5::Widgets Qt5::Core Qt5::Gui)
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} dlib::dlib)
运行Cmake-gui,源文件和目标文件选择不同的目录,会比较干净,后期好维护,如下:
@echo off
rem 获取项目CMakeLists.txt文件所在目录
set CURRENT_CMAKE_DIR=%~dp0
rem 获取项目CMakeLists.txt文件所在目录的名称,用于新建构建目录
for /f "delims=" %%i in ("%cd%") do set CURRENT_DIR_NAME=%%~ni
rem 到项目CMakeLists.txt文件所在目录的上级目录
cd..
rem 获取当前目录,%cd%与%~dp0的区别在于获取的目录尾部少了“\”
set CURRENT_DIR=%cd%
rem 设置需要外部构建的目录名称,在源文件目录后面加“_build”
set PROJECT_NAME=%CURRENT_DIR_NAME%_build
rem 设置外部构建的完整路径
set CURRENT_NEW_DIR=%CURRENT_DIR%\%PROJECT_NAME%
echo Cmake in [%CURRENT_NEW_DIR%]
rem 判断是否已经存在外部构建的路径,如果存在删除
if exist %CURRENT_NEW_DIR% (
rd /s/q %CURRENT_NEW_DIR%
)
rem 新建外部构建目录
mkdir %CURRENT_NEW_DIR%
rem 进入新建的外部构建目录,用于Cmake生成项目文档
cd %CURRENT_NEW_DIR%
rem 根据编译环境需要设定VS版本,%CURRENT_CMAKE_DIR%用于指定CMakeLists.txt所在目录,-G 用于指定makefile的生成工具
::cmake ../ -G "Visual Studio 14 2015 Win64"
::cmake ../ -G "Visual Studio 14 2015 ARM"
cmake %CURRENT_CMAKE_DIR% -G "Visual Studio 14 2015 Win64" -T host=x64
cd..
rem 查看构建项目的目录树
tree %CURRENT_NEW_DIR% /f
pause
等待一段时间后,构建完成。
执行工程,会提示#error "Don't put the dlib folder in your include path"
在release模式下,重新执行工程,这时即可完成工程创建,并将Qt依赖文件复制到目录即可。