cmake vs qmake
- qmake 是为 Qt 量身打造的,使用起来非常方便
- cmake 使用上不如qmake简单直接,但复杂换来的是强大的功能
- 内置的 out-of source 构建。(目前QtCreator为qmake也默认启用了该功能。参考:浅谈 qmake 之 shadow build)
- 为各种平台和场景提供条件编译
- 可处理多个可执行文件情况,和很好配合 QtTest 工作
如何选择?
- 对简单的Qt工程,采用 qmake
- 对复杂度超过 qmake 处理能力的,采用 cmake
使用
准备
在Qt Creator中设置CMake
创建qt工程
运行,发现ok
看一下CMakeLists.txt的内容
cmake_minimum_required(VERSION 3.5)
project(cmake_test LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check http://doc.qt.io/qt-5/deployment-android.html for more information.
# They need to be set before the find_package(Qt5 ...) call.
#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()
find_package(Qt5 COMPONENTS Widgets REQUIRED)
if(ANDROID)
add_library(cmake_test SHARED
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
)
else()
add_executable(cmake_test
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
)
endif()
target_link_libraries(cmake_test PRIVATE Qt5::Widgets)
https://doc.qt.io/qt-5/cmake-get-started.html#build-a-gui-executable
https://doc.qt.io/qt-5.9/cmake-manual.html
https://doc.qt.io/qtcreator/creator-project-cmake.html
https://cmake.org/cmake/help/latest/manual/cmake-qt.7.html