cmake
是自動生成makefile
的工具,編寫txt文件,CMakeLists.txt
,調(diào)用cmake
編譯CMakeLists.txt
來生成
cmake -h
能看到cmake支持的生成的工程模板qtcreator創(chuàng)建一個cmake類型的項目,得到的cmakelists如下
cmake_minimum_required(VERSION 3.14) //指定cmake需求的最低cmake版本project(helloCmake LANGUAGES CXX) //創(chuàng)建項目set(CMAKE_INCLUDE_CURRENT_DIR ON) //set語句,定義變量,為變量賦值,CMAKE_INCLUDE_CURRENT_DIR是cmake自帶的變量,設(shè)置為ON則自動將當(dāng)前源和構(gòu)建目錄添加到包含路徑。默認(rèn)是OFF// https://cmake.org/cmake/help/latest/manual/cmake-variables.7.html 這里可以查看所有的cmake自帶變量set(CMAKE_AUTOUIC ON) //qt相關(guān) 調(diào)用uic.exe把.ui文件編譯成標(biāo)準(zhǔn)c 文件set(CMAKE_AUTOMOC ON) //qt相關(guān) 調(diào)用moc.exe把qt的擴(kuò)展語法(Q_OBJECT)編譯成標(biāo)準(zhǔn)c 語法set(CMAKE_AUTORCC ON) //qt相關(guān) 調(diào)用rcc.exe把資源文件.qrc編譯成標(biāo)準(zhǔn)c 文件set(CMAKE_CXX_STANDARD 11) //設(shè)置c 語言標(biāo)準(zhǔn)set(CMAKE_CXX_STANDARD_REQUIRED ON) //設(shè)置需求最低c 標(biāo)準(zhǔn)# 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()find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Quick LinguistTools REQUIRED)find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick LinguistTools REQUIRED)set(TS_FILES helloCmake_zh_CN.ts)set(PROJECT_SOURCES main.cpp qml.qrc ${TS_FILES})if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) qt_add_executable(helloCmake ${PROJECT_SOURCES} ) qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})else() if(ANDROID) add_library(helloCmake SHARED ${PROJECT_SOURCES} ) else() add_executable(helloCmake ${PROJECT_SOURCES} ) endif() qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})endif()target_compile_definitions(helloCmake PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)target_link_libraries(helloCmake PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Quick)
在qt creator中可以看到編譯過程
qtcreator的編譯不是直接調(diào)用cmake而是為cmake添加了很多參數(shù),而且這些參數(shù)都是以變量的形式存在的 比如 %{Qt:qmakeExecutable}
-GNinja #使用ninja(替代make)-DCMAKE_BUILD_TYPE:String=Debug -DQT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable}-DCMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX}-DCMAKE_C_COMPILER:STRING=%{Compiler:Executable:C}-DCMAKE_CXX_COMPILER:STRING=%{Compiler:Executable:Cxx}
在qt的配置頁面是可以看到這些變量值的。
修改下qtcreator中的參數(shù),用真實值代替qtcreator的變量,下面是用mingw32編譯器的配置,注意最后四條要對應(yīng):c編譯器、c 編譯器、qmake、搜索路徑
-Bbuild
是為了指定生成目錄,要在項目目錄下先創(chuàng)建一個build文件夾-GNinja N要大寫-Bbuild-DCMAKE_BUILD_TYPE:String=Debug-DQT_QMAKE_EXECUTABLE:STRING=C:/Qt/5.15.2/mingw81_32/bin/qmake.exe-DCMAKE_PREFIX_PATH:STRING=C:/Qt/5.15.2/mingw81_32-DCMAKE_C_COMPILER:STRING=C:/Qt/Tools/mingw810_32/bin/gcc.exe-DCMAKE_CXX_COMPILER:STRING=C:/Qt/Tools/mingw810_32/bin/g .exe
注意不要有換行符,上面是為了看起來方便,實際命令如下:
cmake -GNinja -Bbuild -DCMAKE_BUILD_TYPE:String=Debug -DQT_QMAKE_EXECUTABLE:STRING=C:/Qt/5.15.2/mingw81_32/bin/qmake.exe -DCMAKE_PREFIX_PATH:STRING=C:/Qt/5.15.2/mingw81_32 -DCMAKE_C_COMPILER:STRING=C:/Qt/Tools/mingw810_32/bin/gcc.exe -DCMAKE_CXX_COMPILER:STRING=C:/Qt/Tools/mingw810_32/bin/g .exe
聯(lián)系客服