目录
前提
cmake和qt都需要配置环境变量 ,不然报错真的烦啊
VS里的cmake简易版
#请求CMake的版本
cmake_minimum_required(VERSION 3.8)
#指定项目名称
project(QtPro)
#指定C++标准为17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#在MSVC上必须指定 /Zc:__cplusplus 选项
if(MSVC)
add_compile_options(/Zc:__cplusplus)
endif()
#指定头文件包含目录
include_directories("D:/software/Qt/6.2.4/msvc2019_64/include")
#查找指定的模块(相当于声明)
find_package(Qt6 COMPONENTS Widgets REQUIRED)
#指定目标生成位置
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/bin)
#根据指定的文件生成可执行程序
add_executable(QtPro "main.cpp")
#给目标连接库文件 需要add_executable之后
target_link_libraries(QtPro Qt6::Widgets)
#include <QApplication>
#include<QWidget>
//在windows平台去掉黑窗口
#pragma comment(linker,"/subSystem:windows /entry:mainCRTStartup")
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
w.show();
return a.exec();
}
QT里的cmake解析
#请求cmake执行的最小的版本
cmake_minimum_required(VERSION 3.5)
#指定项目信息 项目名称 项目版本 cxx代表c++
project(CmakeProject VERSION 0.1 LANGUAGES CXX)
#自动uic moc rcc
set(CMAKE_AUTOUIC ON) #写ui文件的时候 会自动生成源代码
set(CMAKE_AUTOMOC ON) #是否自动元编译
set(CMAKE_AUTORCC ON) #是否自动编译资源文件
#设置c++标准为17
set(CMAKE_CXX_STANDARD 17)
#强制设置c++标准,如果不支持c++17则会发生错误
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#查找模块
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
#项目文件(代码 资源 ui文件)
set(PROJECT_SOURCES
main.cpp
widget.cpp
widget.h
)
#如果版本大于等于qt6则生成可执行程序(MANUAL_FINALIZATION 必须手动结束生成)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(CmakeProject
MANUAL_FINALIZATION
hello.h hello.cpp
${PROJECT_SOURCES}
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET CmakeProject APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else() #如果是安卓平台
if(ANDROID)
add_library(CmakeProject SHARED
${PROJECT_SOURCES}
)
# Define properties for Android with Qt 5 after find_package() calls as:
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
else()
add_executable(CmakeProject
${PROJECT_SOURCES}
)
endif()
endif()
#给目标连接库
target_link_libraries(CmakeProject PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.CmakeProject)
endif()
#设置目标的属性
set_target_properties(CmakeProject PROPERTIES
${BUNDLE_ID_OPTION}
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
include(GNUInstallDirs)
install(TARGETS CmakeProject
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
#终止生成
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(CmakeProject)
endif()