QMake 工程 转换为CMake 工程
【摘要】 QMake 工程 转换为CMake 工程
源码目录结构
.
├── build.sh
├── businesscontrol.cpp
├── businesscontrol.h
├── globaldef.h
├── lib
│ └── x86_64
├── log
│ ├── LogFileManager.cpp
│ ├── LogFileManage...
QMake 工程 转换为CMake 工程
源码目录结构
.
├── 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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
QMake Demo
#-------------------------------------------------
#
# 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 +=
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
CMake Demo
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})
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
QMake CMake 对照转换表
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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
TARGET = demo
TEMPLATE = app
------------------------------
project(demo LANGUAGES CXX) #项目名,语言 C++
- 1
- 2
- 3
- 4
DEFINES += QT_MESSAGELOGCONTEXT
--------------------------------
set(QT_MESSAGELOGCONTEXT TRUE) # Qt消息打开,默认关闭。需要打开QLog release/debug 消息打印相关
add_definitions(-DQT_MESSAGELOGCONTEXT) #添加QtMessage 宏定义,默认关闭
- 1
- 2
- 3
- 4
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()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
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
)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
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
)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
#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")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
INCLUDEPATH += $$QT_SYSROOT/usr/local/include/***** \
------------------------------------------------------
include_directories( /usr/local/include/****
)
- 1
- 2
- 3
- 4
- 5
LIBS += -lpaho-mqtt3a
---------------------------
set(PROJECT_LIBS paho-mqtt3a )
- 1
- 2
- 3
- 4
- 5
其他的可参考Qt6.0CMake工程生成时默认文件内容。
文章来源: blog.csdn.net,作者:何其不顾四月天,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/u011218356/article/details/117218332
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)