QT的cmake项目工程配置
Posted 特立独行的猫a
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QT的cmake项目工程配置相关的知识,希望对你有一定的参考价值。
QT的项目工程默认使用的是qmake,这本来也没什么问题。但是由于要用到vcpkg这个工具来管理第三方库,好像这个 vcpkg工具只能在cmake的项目中才能使用。
于是这里记录下QT的cmake工程配置,需要注意的是需要额外配置个项:
-DCMAKE_PREFIX_PATH=D:\\Qt\\Qt5.10.0\\5.10.0\\msvc2015\\lib\\cmake
-DCMAKE_TOOLCHAIN_FILE=F:\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake -Wno-dev
另外需要注意的是,在QT的环境中可以直接运行能出来界面没问题。
在其他环境下,得先记得先打包导出才行! 之前就是忘了这个差点以为代码配置问题。
Qml的打包和QT传统的打包方式也是不同的。
QT的打包可以用这个:
D:\\Qt\\Qt5.10.0\\5.10.0\\msvc2015\\bin\\windeployqt.exe -no-angle -no-opengl-sw test.exe
QML的应用打包,通过cd命令到你的exe存放的地方,并输入下面的命令:
windeployqt -qmldir "源码路径" test.exe --release
命令执行完后,在你exe生成的目录里生成qt的依赖文件。 如果你的源码使用了三方库或者生成了动态链接库,那么需要手动将需要的dll文件复制到目录下,实际运行试试,是不是可以正常运行。
CmakeLists.txt 配置模板如下:
cmake_minimum_required(VERSION 3.12)
project(test VERSION 0.3.19)
#宏定义在这里添加
add_definitions(
-D_ENABLE_LOGGING
)
#################### QT dependencies ####################
#QT相关的配置
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(QT_VERSION 5)
set(REQUIRED_LIBS Core Quick Widgets)
set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Quick Qt5::Widgets)
#################### set output directory ####################
set(BUILD_DIR $CMAKE_CURRENT_SOURCE_DIR/../../build)
set(LIB_DIR $BUILD_DIR/lib/Release)
set(LIB_FIX)
if (CMAKE_BUILD_TYPE MATCHES "Debug")
set(LIB_DIR $BUILD_DIR/lib/Debug)
set(LIB_FIX _d)
endif ()
get_filename_component(ABSOLUTE_PATH $LIB_DIR ABSOLUTE)
set(LIB_DIR $ABSOLUTE_PATH)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY $LIB_DIR)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY $LIB_DIR)
set(CMAKE_PDB_OUTPUT_DIRECTORY $LIB_DIR)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY $LIB_DIR)
set(LIB_DIR_FIX $LIB_DIR)
option(USE_VS_BUILD "use visual studio build." OFF)
if (USE_VS_BUILD)
set(LIB_DIR_FIX $LIB_DIR/bin/Debug)
endif ()
#################### set include path ####################
include_directories(
$CMAKE_CURRENT_SOURCE_DIR/source/cpp/mydir1
$CMAKE_CURRENT_SOURCE_DIR/source/cpp/mydir2
$CMAKE_CURRENT_SOURCE_DIR/source/cpp
$BUILD_DIR/include
)
#################### scan source files ####################
aux_source_directory($CMAKE_CURRENT_SOURCE_DIR/source/cpp/mydir1 SRC_FILES)
aux_source_directory($CMAKE_CURRENT_SOURCE_DIR/source/cpp/mydir2 SRC_FILES)
aux_source_directory($CMAKE_CURRENT_SOURCE_DIR/source/cpp SRC_FILES)
set(RESOURCE_SOURCES
source/qml/qml.qrc
# source/image/image.qrc
)
#set(QML_SOURCES
# source/qml/main.qml)
#################### version config ####################
if (MSVC)
set(MY_VERSIONINFO_RC "$CMAKE_CURRENT_BINARY_DIR/VersionInfo.rc")
configure_file("$CMAKE_CURRENT_SOURCE_DIR/resource.rc.in"
"$MY_VERSIONINFO_RC")
endif ()
add_executable($PROJECT_NAME $SRC_FILES $QML_SOURCES $RESOURCE_SOURCES $MY_VERSIONINFO_RC)
#################### set target properties ####################
set_target_properties($PROJECT_NAME PROPERTIES DEBUG_POSTFIX _d)
set_property(TARGET $PROJECT_NAME PROPERTY WIN32_EXECUTABLE true)
#################### set target dependencies ####################
find_package(Qt$QT_VERSION COMPONENTS $REQUIRED_LIBS REQUIRED)
find_package(g3log CONFIG REQUIRED)
###################其他第三方库链接配置##############################
set(MY_CLIENT_LIB $LIB_DIR_FIX/mymodule1$LIB_FIX.lib)
set(OTHER_CLIENT_LIB $LIB_DIR_FIX/libothermodule$LIB_FIX.lib)
set(THIRD_LIBS
g3log
$MY_CLIENT_LIB
$OTHER_CLIENT_LIB
)
target_link_libraries($PROJECT_NAME PRIVATE $REQUIRED_LIBS_QUALIFIED $THIRD_LIBS)
过程:
"D:\\Program Files\\JetBrains\\CLion 2021.3.3\\bin\\cmake\\win\\bin\\cmake.exe" -DCMAKE_BUILD_TYPE=Debug "-DCMAKE_MAKE_PROGRAM=D:/Program Files/JetBrains/CLion 2021.3.3/bin/ninja/win/ninja.exe" -DCMAKE_PREFIX_PATH=D:\\Qt\\Qt5.10.0\\5.10.0\\msvc2015\\lib\\cmake -DCMAKE_TOOLCHAIN_FILE=F:\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake -Wno-dev -G Ninja E:\\work\\test
-- The C compiler identification is MSVC 19.0.24215.1
-- The CXX compiler identification is MSVC 19.0.24215.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/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 14.0/VC/bin/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - not found
-- Found Threads: TRUE
-- Found G3LOG: F:/vcpkg/installed/x86-windows/include
-- Configuring done
-- Generating done
-- Build files have been written to: E:/work/test/cmake-build-debug
[Finished]
引用:
配置CLion作为Qt5开发环境 - apocelipes - 博客园
以上是关于QT的cmake项目工程配置的主要内容,如果未能解决你的问题,请参考以下文章
windows下编译google-protocolbuf在Qt中使用
Android OpenCVVisual Studio 创建支持 OpenCV 库的 CMake 工程 ② ( VS 中创建 CMake 工程 | CMake 工程中配置 OpenCV 头文件 )