QMake 工程 转换为CMake 工程

2023-03-10 13:13:55 浏览数 (1)

QMake 工程 转换为CMake 工程

源码目录结构

代码语言:javascript复制
.
├── build.sh
├── businesscontrol.cpp
├── businesscontrol.h
├── globaldef.h
├── lib
│   └── x86_64
├── log
│   ├── LogFileManager.cpp
│   ├── LogFileManager.h
├── main.cpp
├── mqtt
│   ├── mqttclient.cpp
│   ├── mqttclient.h
├── demo.pro
├── CMakeList.txt
├── res
│   ├── closea.png
│   ├── closeb.png
│   ├── close.png
├── tapplication.cpp
├── tapplication.h
├── util
│   ├── json.cpp
│   ├── json.h
└── widget
    ├── mainwidget.cpp
    ├── mainwidget.h

QMake Demo

代码语言:javascript复制
#-------------------------------------------------
#
# Project created by QtCreator 2021-01-04T09:37:29
#
#-------------------------------------------------

QT        = core gui

greaterThan(QT_MAJOR_VERSION, 4): QT  = widgets

TARGET = demo
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES  = QT_DEPRECATED_WARNINGS
DEFINES  = LINUX_OS_VERSION==$$QT_ARCH
DEFINES  = QT_MESSAGELOGCONTEXT

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES  = QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

message($$QT_ARCH)
message($$QT_VERSION)

contains(QT_ARCH, x86_64){
    message("LINUX_OS_X86_64")
    DEFINES  = LINUX_OS_X86_64
}else{
    message("LINUX_OS_ARM64")
    DEFINES  = LINUX_OS_ARM64
}

exists (../.git) {
    GIT_BRANCH   = $$system(git rev-parse --abbrev-ref HEAD)
    GIT_TIME     = $$system(git show --oneline --format="%ci%H" -s HEAD)

    APP_VERSION = "VersionInfo: $${GIT_BRANCH} : $${GIT_TIME}"
} else {
    APP_VERSION     = None
}
DEFINES  = APP_VERSION="\"$$APP_VERSION\""
message($$APP_VERSION)

INCLUDEPATH  = widget 
    log
    mqtt
    util

SOURCES  = 
    main.cpp 
    tapplication.cpp 
    log/LogFileManager.cpp 
    widget/mainwidget.cpp 
    mqtt/mqttclient.cpp 
    util/json.cpp 
    businesscontrol.cpp 
HEADERS  = 
    tapplication.h 
    widget/mainwidget.h 
    log/LogFileManager.h 
    globaldef.h 
    mqtt/mqttclient.h 
    util/json.h 
    businesscontrol.h     
FORMS  = 
    widget/toolbar.ui 

#LIBRARY options
QMAKE_CC  = -g
QMAKE_CXX  = -g
QMAKE_LINK  = -g

message($$PWD)
message($$QT_SYSROOT)

INCLUDEPATH  = $$QT_SYSROOT/usr/local/include/***** 

LIBS  =  -lpaho-mqtt3a 

DISTFILES  =

CMake Demo

代码语言:javascript复制
cmake_minimum_required(VERSION 3.5)

project(demo LANGUAGES CXX) #项目名,语言 C  

set(CMAKE_INCLUDE_CURRENT_DIR ON) #当前include 打开

set(CMAKE_AUTOUIC ON)  # ui转换支持打开
set(CMAKE_AUTOMOC ON)  # moc转换支持打开 
set(CMAKE_AUTORCC ON)  # 资源文件支持 打开

set(CMAKE_CXX_STANDARD 11) # C  11
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check https://doc.qt.io/qt/deployment-android.html for more information.
# They need to be set before the find_package( ...) calls below.

#if(ANDROID)
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
#    if (ANDROID_ABI STREQUAL "armeabi-v7a")
#        set(ANDROID_EXTRA_LIBS
#            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
#            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
#    endif()
#endif()

set(QT_MESSAGELOGCONTEXT TRUE) # Qt消息打开,默认关闭。需要打开QLog release/debug 消息打印相关
add_definitions(-DQT_MESSAGELOGCONTEXT) #添加QtMessage 宏定义,默认关闭

#Git相关(可删除)
set(GIT_COMMIT_HASH "")
set(GIT_BRANCH_NAME "")
find_package(Git QUIET)
if(GIT_FOUND)
execute_process(
    #Format %ci time yyyy-mm-dd hh:mm:ss %H commitID
    COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:%ci%H
    OUTPUT_VARIABLE GIT_COMMIT_HASH
    OUTPUT_STRIP_TRAILING_WHITESPACE
    ERROR_QUIET
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
execute_process(
    COMMAND ${GIT_EXECUTABLE} symbolic-ref --short -q HEAD
    OUTPUT_VARIABLE GIT_BRANCH_NAME
    OUTPUT_STRIP_TRAILING_WHITESPACE
    ERROR_QUIET
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
endif()
set(APP_VERSION "VersionInfo: ${GIT_BRANCH_NAME}:${GIT_COMMIT_HASH}")
message(STATUS  ${APP_VERSION}) 

configure_file(
  ${CMAKE_CURRENT_SOURCE_DIR}/ver.h.ini
  ${CMAKE_BINARY_DIR}/ver.h
  @ONLY
)
#Git相关,默认没有,可删除

#系统信息打印(可删除)
message(STATUS  "cmake-version:" ${CMAKE_VERSION}) #CMake版本信息
message(STATUS  "system_name:" ${CMAKE_HOST_SYSTEM_NAME}) #系统名称
message(STATUS  "system_version:" ${CMAKE_HOST_SYSTEM_PROCESSOR}) #系统版本
message(STATUS  "system_kernel:" ${CMAKE_HOST_SYSTEM_VERSION}) #系统内核版本

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -O2")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -O2")

if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64") #Linux 平台判断 x86还是arm64(可删除)
    message(STATUS "LINUX_OS_X86_64")
    set(LINUX_OS_X86_64 TRUE)
    add_definitions(-DLINUX_OS_X86_64)
elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "AARCH64")
    message(STATUS "LINUX_OS_ARM64")
    set(LINUX_OS_ARM64 TRUE)
    add_definitions(-DLINUX_OS_ARM64)
endif()
#头文件包含
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/widget
                    ${CMAKE_CURRENT_SOURCE_DIR}/log
                    ${CMAKE_CURRENT_SOURCE_DIR}/mqtt
                    ${CMAKE_CURRENT_SOURCE_DIR}/util
)
#系统头文件包含
include_directories(
                    /usr/local/include/****
)
#库查找
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
#find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Gui REQUIRED)
#find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED)
#源文件包含
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} PROJECT_SOURCES)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/widget WIDGET_SOURCES)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/mqtt MQTT_SOURCES)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/util UTIL_SOURCES)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/log LOG_SOURCES)
#第三方链接库包含
set(PROJECT_LIBS
        paho-mqtt3a                    
)
#可执行文件生成
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(demo
            ${PROJECT_SOURCES} ${WIDGET_SOURCES} ${MQTT_SOURCES} ${LOG_SOURCES} ${UTIL_SOURCES} ${SPICEUSB_SOURCES}
    )
else()
    if(ANDROID)
        add_library(demo SHARED
            ${PROJECT_SOURCES} ${WIDGET_SOURCES} ${MQTT_SOURCES} ${LOG_SOURCES} ${UTIL_SOURCES} ${SPICEUSB_SOURCES}
        )
    else()
        add_executable(demo
            ${PROJECT_SOURCES} ${WIDGET_SOURCES} ${MQTT_SOURCES} ${LOG_SOURCES} ${UTIL_SOURCES} ${SPICEUSB_SOURCES}
        )
    endif()
endif()
#库文件链接
target_link_libraries(demo PRIVATE Qt${QT_VERSION_MAJOR}::Widgets ${PROJECT_LIBS})

QMake CMake 对照转换表

代码语言:javascript复制
QT        = core gui
greaterThan(QT_MAJOR_VERSION, 4): QT  = widgets
------------------------------------
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
#find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Gui REQUIRED)
#find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED)
代码语言:javascript复制
TARGET = demo
TEMPLATE = app
------------------------------
project(demo LANGUAGES CXX) #项目名,语言 C  
代码语言:javascript复制
DEFINES  = QT_MESSAGELOGCONTEXT
--------------------------------
set(QT_MESSAGELOGCONTEXT TRUE) # Qt消息打开,默认关闭。需要打开QLog release/debug 消息打印相关
add_definitions(-DQT_MESSAGELOGCONTEXT) #添加QtMessage 宏定义,默认关闭
代码语言:javascript复制
DEFINES  = LINUX_OS_VERSION==$$QT_ARCH
message($$QT_ARCH)
message($$QT_VERSION)

contains(QT_ARCH, x86_64){
    message("LINUX_OS_X86_64")
    DEFINES  = LINUX_OS_X86_64
}else{
    message("LINUX_OS_ARM64")
    DEFINES  = LINUX_OS_ARM64
}
---------------------------------
message(STATUS  "cmake-version:" ${CMAKE_VERSION}) #CMake版本信息
message(STATUS  "system_name:" ${CMAKE_HOST_SYSTEM_NAME}) #系统名称
message(STATUS  "system_version:" ${CMAKE_HOST_SYSTEM_PROCESSOR}) #系统版本
message(STATUS  "system_kernel:" ${CMAKE_HOST_SYSTEM_VERSION}) #系统内核版本
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64") #Linux 平台判断 x86还是arm64
    message(STATUS "LINUX_OS_X86_64")
    set(LINUX_OS_X86_64 TRUE)
    add_definitions(-DLINUX_OS_X86_64)
elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "AARCH64")
    message(STATUS "LINUX_OS_ARM64")
    set(LINUX_OS_ARM64 TRUE)
    add_definitions(-DLINUX_OS_ARM64)
endif()
代码语言:javascript复制
exists (../.git) {
    GIT_BRANCH   = $$system(git rev-parse --abbrev-ref HEAD)
    GIT_TIME     = $$system(git show --oneline --format="%ci%H" -s HEAD)

    APP_VERSION = "VersionInfo: $${GIT_BRANCH} : $${GIT_TIME}"
} else {
    APP_VERSION     = None
}
DEFINES  = APP_VERSION="\"$$APP_VERSION\""
message($$APP_VERSION)
--------------------------------------------------------------
set(GIT_COMMIT_HASH "")
set(GIT_BRANCH_NAME "")
find_package(Git QUIET)
if(GIT_FOUND)
execute_process(
    #Format %ci time yyyy-mm-dd hh:mm:ss %H commitID
    COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:%ci%H
    OUTPUT_VARIABLE GIT_COMMIT_HASH
    OUTPUT_STRIP_TRAILING_WHITESPACE
    ERROR_QUIET
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
execute_process(
    COMMAND ${GIT_EXECUTABLE} symbolic-ref --short -q HEAD
    OUTPUT_VARIABLE GIT_BRANCH_NAME
    OUTPUT_STRIP_TRAILING_WHITESPACE
    ERROR_QUIET
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
endif()
set(APP_VERSION "VersionInfo: ${GIT_BRANCH_NAME}:${GIT_COMMIT_HASH}")
message(STATUS  ${APP_VERSION}) 

configure_file(
  ${CMAKE_CURRENT_SOURCE_DIR}/ver.h.ini
  ${CMAKE_BINARY_DIR}/ver.h
  @ONLY
)
代码语言:javascript复制
INCLUDEPATH  = widget 
    log
    mqtt 
    util
-----------------------------------
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/widget
                    ${CMAKE_CURRENT_SOURCE_DIR}/log
                    ${CMAKE_CURRENT_SOURCE_DIR}/mqtt
                    ${CMAKE_CURRENT_SOURCE_DIR}/util
)
代码语言:javascript复制
SOURCES  = 
    main.cpp 
    tapplication.cpp 
    log/LogFileManager.cpp 
    widget/mainwidget.cpp 
    mqtt/mqttclient.cpp 
    util/json.cpp 
    businesscontrol.cpp 
HEADERS  = 
    tapplication.h 
    widget/mainwidget.h 
    log/LogFileManager.h 
    globaldef.h 
    mqtt/mqttclient.h 
    util/json.h 
    businesscontrol.h     
FORMS  = 
    widget/toolbar.ui 
------------------------------------------
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} PROJECT_SOURCES)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/widget WIDGET_SOURCES)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/mqtt MQTT_SOURCES)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/util UTIL_SOURCES)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/log LOG_SOURCES)
代码语言:javascript复制
#LIBRARY options
QMAKE_CC  = -g
QMAKE_CXX  = -g
QMAKE_LINK  = -g
-----------------------------
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -O2")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -O2")
代码语言:javascript复制
INCLUDEPATH  = $$QT_SYSROOT/usr/local/include/***** 
------------------------------------------------------
include_directories(
                    /usr/local/include/****
)
代码语言:javascript复制
LIBS  =  -lpaho-mqtt3a 
---------------------------
set(PROJECT_LIBS
        paho-mqtt3a                    
)

其他的可参考Qt6.0CMake工程生成时默认文件内容。

0 人点赞