QT的cmake项目工程配置

2022-04-13 10:16:13 浏览数 (1)

QT的项目工程默认使用的是qmake,这本来也没什么问题。但是由于要用到vcpkg这个工具来管理第三方库,好像这个 vcpkg工具只能在cmake的项目中才能使用。

于是这里记录下QT的cmake工程配置,需要注意的是需要额外配置个项:

代码语言:javascript复制
-DCMAKE_PREFIX_PATH=D:QtQt5.10.05.10.0msvc2015libcmake 
-DCMAKE_TOOLCHAIN_FILE=F:vcpkgscriptsbuildsystemsvcpkg.cmake -Wno-dev

另外需要注意的是,在QT的环境中可以直接运行能出来界面没问题。

在其他环境下,得先记得先打包导出才行! 之前就是忘了这个差点以为代码配置问题。

Qml的打包和QT传统的打包方式也是不同的。

QT的打包可以用这个:

代码语言:javascript复制
D:QtQt5.10.05.10.0msvc2015binwindeployqt.exe -no-angle -no-opengl-sw test.exe

QML的应用打包,通过cd命令到你的exe存放的地方,并输入下面的命令:

代码语言:javascript复制
windeployqt  -qmldir   "源码路径"     test.exe   --release

命令执行完后,在你exe生成的目录里生成qt的依赖文件。 如果你的源码使用了三方库或者生成了动态链接库,那么需要手动将需要的dll文件复制到目录下,实际运行试试,是不是可以正常运行。

CmakeLists.txt 配置模板如下:

这里面每一项的含义,好像都能看懂。总之比makefile好看多了,再也不愿回到makefile了。

需要多总结多记,看的多用的多了就熟了吧。

预定义变量

PROJECT_NAME项目名称 PROJECT_SOURCE_DIR工程的根目录 PROJECT_BINARY_DIR 执行cmake命令的目录 PROJECT_BINARY_DIR 执行cmake命令的目录 CMAKE_CURRENT_SOURCE_DIR 当前CMakeLists.txt文件所在目录 CMAKE_C_FLAGS设置C编译选项 CMAKE_CXX_FLAGS设置C 编译选项 CMAKE_C_COMPILER设置C编译器 CMAKE_CXX_COMPILER设置C 编译器 EXECUTABLE_OUTPUT_PATH设置编译后可执行文件目录 LIBRARY_OUTPUT_PATH设置生成的库文件目录

常用规则

cmake_minimum_required(VERSION 3.16) 指令cmake 版本project(hello_world) 设置工程名include_directories({PROJECT_SOURCE_DIR}/include) 添加头文件路径link_directories({PROJECT_SOURCE_DIR}/lib) 添加链接库的路径add_subdirectory(module)添加 module 子目录, 此目录下也要有CMakeLists.txt文件add_executable(project1 main.c)指定编译的可执行文件add_library(lib1 SHARED library.c library.h)指定生成的库文件,SHARED是生成动态库,STATIC后生成静态库 add_compile_options() 添加编译选项target_link_libraries()指定动态链接库install()指定make install的目录

set(XXXX YYYYYY)用于设置和修改变量 ${XXXX} 使用变量

代码语言:javascript复制
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})

过程:

代码语言:javascript复制
"D:Program FilesJetBrainsCLion 2021.3.3bincmakewinbincmake.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:QtQt5.10.05.10.0msvc2015libcmake -DCMAKE_TOOLCHAIN_FILE=F:vcpkgscriptsbuildsystemsvcpkg.cmake -Wno-dev -G Ninja E:worktest
-- 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 - 博客园

0 人点赞