qmake介绍
只需要在.pro文件写入几行配置信息,qmake工具就能自动生成Makefile文件。qmake包含支持Qt开发的附加特性,包含moc和uic的构建规则,qmake还可以为Microsoft Visual studio生成项目,且不需要开发人员更改项目文件。
qmake使用
生成Makefile:qmake -o Makefile hello.pro
生成VS项目文件:qmake -tp vc hello.pro
生成Xcode项目文件:qmake -spec macx-xcode hello.pro
设置属性-值:qmake -set property value
查询属性:qmake -query property
.pro写法
#要使用的Qt模块
QT += network xml
#要构建的项目类型
TEMPLATE = app
#输出程序名称
TARGET = myapp
#程序输出目录
DESTDIR = ./mybin
#配置:包含调试信息
CONFIG += debug
#定义宏
DEFINES += MY_STUFF
#头文件
HEADERS += hello.h
#源文件
SOURCES += hello.cpp \
main.cpp
#外部头文件
INCLUDEPATH = c:/msdev/include d:/stl/include
#链接外部库
LIBS += -L/usr/local/lib -lmath
#win平台
win32 {
SOURCES += hellowin.cpp
}
#unix平台
unix {
SOURCES += hellounix.cpp
}
#win平台并且调试模式(嵌套作用域)
win32:debug {
CONFIG += console
}
#函数举例
eval(TARGET = myapp) {
message($$TARGET)
}
options = $$find(CONFIG, "debug") $$find(CONFIG, "release")
count(options, 2) {
message(Both release and debug specified.)
}
!exists( main.cpp ) {
error( "No main.cpp file found" )
}
qmake语法
#赋值:=
TARGET = app
#追加:+=
DEFINES += MY_STUFF
#移除:-=
DEFINES -= MY_STUFF
#唯一值:*=
#例子中只有MY_STUFF不存在才会添加,避免重复
DEFINES *= MY_STUFF
#替换:~=
#例子中DEFINES里以QT_D或QT_T开头的任何值都被替换为QT
DEFINES ~= s/QT_[DT].+/QT
#操作符$$用于提取变量的值,并可用于在变量之间传递值或向函数提供值
#1 提取变量值
EVERYTHING = $$SOURCES $$HEADERS
message("The project contains the following files:")
message($$EVERYTHING)
#2 在qmake运行时获取环境变值的值
DESTDIR = $$(PWD)
message(The project will be installed in $$DESTDIR)
#3 在生成Makefile时获取环境变量的值
DESTDIR = $(PWD)
message(The project will be installed in the value of PWD)
message(when the Makefile is processed.)
#4 可以将变量附加到另一个值而不需要空格
TARGET = myproject_$${TEMPLATE}
#操作符$$[]用于获取qmake的属性
message(Qt version: $$[QT_VERSION])
message(Qt is installed in $$[QT_INSTALL_PREFIX])
message(Binary files (executables): $$[QT_INSTALL_BINS])
#作用域
#作用域写为条件(可以嵌套),后面跟着一对大括号中包含的一系列声明
win32 {
SOURCES += paintwidget_win.cpp
}
win32:DEFINES += MY_STUFF
macx {
CONFIG(debug, debug|release) {
HEADERS += debugging.h
}
}
macx:CONFIG(debug, debug|release) {
HEADERS += debugging.h
}
#win或mac一个满足即可
win32|macx {
HEADERS += debugging.h
}
if(win32|macos):CONFIG(debug, debug|release) {
# Do something on Windows and macOS,
# but only for the debug configuration.
}
win32|if(macos:CONFIG(debug, debug|release)) {
# Do something on Windows (regardless of debug or release)
# and on macOS (only for debug).
}
#使用通配符
win32-* {
# Matches every mkspec starting with "win32-"
SOURCES += win32_specific.cpp
}
#使用else
win32:xml {
message(Building for Windows)
SOURCES += xmlhandler_win.cpp
} else:xml {
SOURCES += xmlhandler.cpp
} else {
message("Unknown configuration")
}
#CONFIG里每个值都可以用作作用域的条件
CONFIG += opengl
opengl {
TARGET = application-gl
} else {
TARGET = application
}
#各种其他内置平台和特定于编译器的值也可以用作用域
message($$QMAKESPEC)
linux-g++ {
message(Linux)
}
#自定义变量
MY_VARIABLE = value
#以下两种写法等效
MY_DEFINES = $$DEFINES
MY_DEFINES = $${DEFINES}
预编译头
// stable.h
// Add C includes here
#if defined __cplusplus
// Add C++ includes here
#include <stdlib>
#include <iostream>
#include <vector>
#include <QApplication> // Qt includes
#include <QPushButton>
#include <QLabel>
#include "thirdparty/include/libmain.h"
#include "my_stable_class.h"
...
#endif
使用预编译头文件:在.pro加入PRECOMPILED_HEADER = mystable.h
预编译条件块:precompile_header:!isEmpty(PRECOMPILED_HEADER) { DEFINES += USING_PCH }
qmake变量
ANDROID_ABI
ANDROID_ABIS
ANDROID_API_VERSION
ANDROID_BUNDLED_JAR_DEPENDENCIES
ANDROID_DEPLOYMENT_DEPENDENCIES
ANDROID_DEPLOYMENT_SETTINGS_FILE
ANDROID_EXTRA_LIBS
ANDROID_EXTRA_PLUGINS
ANDROID_FEATURES
ANDROID_LIB_DEPENDENCIES
ANDROID_MIN_SDK_VERSION
ANDROID_PACKAGE_SOURCE_DIR
ANDROID_PERMISSIONS
ANDROID_TARGET_SDK_VERSION
ANDROID_VERSION_CODE
ANDROID_VERSION_NAME
CONFIG
DEFINES
DEFINES_DEBUG
DEFINES_RELEASE
DEF_FILE
DEPENDPATH
DESTDIR
DISTFILES
DLLDESTDIR
EXTRA_TRANSLATIONS
FORMS
GUID
HEADERS
ICON
IDLSOURCES
INCLUDEPATH
INSTALLS
JAVA_HOME
LEXIMPLS
LEXOBJECTS
LEXSOURCES
LIBS
LIBS_PRIVATE
LITERAL_HASH
MAKEFILE
MAKEFILE_GENERATOR
MSVCPROJ_*
MOC_DIR
OBJECTIVE_HEADERS
OBJECTIVE_SOURCES
OBJECTS
OBJECTS_DIR
POST_TARGETDEPS
PRE_TARGETDEPS
PRECOMPILED_HEADER
PWD
OUT_PWD
QM_FILES_RESOURCE_PREFIX
QM_FILES_INSTALL_PATH
QML_IMPORT_PATH
QMAKE
QMAKESPEC
QMAKE_AR_CMD
QMAKE_BUNDLE_DATA
QMAKE_BUNDLE_EXTENSION
QMAKE_CC
QMAKE_CFLAGS
QMAKE_CFLAGS_DEBUG
QMAKE_CFLAGS_RELEASE
QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO
QMAKE_CFLAGS_SHLIB
QMAKE_CFLAGS_THREAD
QMAKE_CFLAGS_WARN_OFF
QMAKE_CFLAGS_WARN_ON
QMAKE_CLEAN
QMAKE_CXX
QMAKE_CXXFLAGS
QMAKE_CXXFLAGS_DEBUG
QMAKE_CXXFLAGS_RELEASE
QMAKE_CXXFLAGS_RELEASE_WITH_DEBUGINFO
QMAKE_CXXFLAGS_SHLIB
QMAKE_CXXFLAGS_THREAD
QM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_THREAD
QMAKE_LIBS_X11
QMAKE_LIB_FLAG
QMAKE_LINK
QMAKE_LINK_SHLIB_CMD
QMAKE_LN_SHLIB
QMAKE_LRELEASE_FLAGS
QMAKE_OBJECTIVE_CFLAGS
QMAKE_POST_LINK
QMAKE_PRE_LINK
QMAKE_PROJECT_NAME
QMAKE_PROVISIONING_PROFILE
QMAKE_MAC_SDK
QMAKE_MACOSX_DEPLOYMENT_TARGET
QMAKE_MAKEFILE
QMAKE_QMAKE
QMAKE_RESOURCE_FLAGS
QMAKE_RPATHDIR
QMAKE_RPATHLINKDIR
QMAKE_RUN_CC
QMAKE_RUN_CC_IMP
QMAKE_RUN_CXX
QMAKE_RUN_CXX_IMP
QMAKE_SONAME_PREFIX
QMAKE_TARGET
QMAKE_TARGET_COMPANY
QMAKE_TARGET_DESCRIPTION
QMAKE_TARGET_COPYRIGHT
QMAKE_TARGET_PRODUCT
QMAKE_MANIFEST
QMAKE_TVOS_DEPLOYMENT_TARGET
QMAKE_UIC_FLAGS
QMAKE_WATCHOS_DEPLOYMENT_TARGET
QML_IMPORT_MAJOR_VERSION
QML_IMPORT_MINOR_VERSION
QML_IMPORT_VERSION
QML_IMPORT_NAME
QML_FOREIGN_METATYPES
QT
QTPLUGIN
QT_VERSION
QT_MAJOR_VERSION
QT_MINOR_VERSION
QT_PATCH_VERSION
RC_FILE
RC_CODEPAGE
RC_DEFINES
RC_ICONS
RC_LANG
RC_INCLUDEPATH
RCC_DIR
REQUIRES
RESOURCES
RES_FILE
SOURCES
SUBDIRS
TARGET
TARGET_EXT
TARGET_x
TARGET_x.y.z
TEMPLATE
TRANSLATIONS
UI_DIR
VERSION
VERSION_PE_HEADER
VER_MAJ
VER_MIN
VER_PAT
VPATH
WINDOWS_TARGET_PLATFORM_VERSION
WINDOWS_TARGET_PLATFORM_MIN_VERSION
WINRT_MANIFEST
YACCSOURCES
_PRO_FILE_
_PRO_FILE_PWD_