1.新建项目
2.打开项目,如果出现报错
By not providing “FindQt5.cmake” in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by “Qt5”, but CMake did not find one.
解决方法:安装QT5和对应的cmake软件,然后修改makelist配置文件。
CMake安装位置:
QT安装位置:
3.配置CMake
4.配置QT位置
set(CMAKE_PREFIX_PATH “D:/Library/Qt5.14.2/5.14.2/msvc2017_64/lib/cmake”)
5.配置项目生成平台为64
6.重新加载项目
7.运行
QA
Q:Error copying file “D:/Library/Qt5.14.2/5.14.2/msvc2017_64/lib/cmake/Qt5/…/…/bin/Qt5Cored.dll” to “E:/092-CPP/myqt/cmake-build-debug”.
NMAKE : fatal error U1077: “echo”: 返回代码“0x1”
A:检查路径是否正确,set(CMAKE_PREFIX_PATH “D:/Library/Qt5.14.2/5.14.2/msvc2017_64/lib/cmake”)
默认CMakeLists文件
#工程名
project(uXXXSoftware)
#cmake最低版本
cmake_minimum_required(VERSION 3.17)
#C++标准
set(CMAKE_CXX_STANDARD 11)
# 开启QT用于预处理的组件
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
#配置QT位置
set(CMAKE_PREFIX_PATH "D:/Library/Qt5.14.2/5.14.2/msvc2017_64/lib/cmake")
# 查找QT的模块
find_package(Qt5 COMPONENTS
Core
Gui
Widgets
REQUIRED)
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
# 添加源文件
add_executable(uChangAnSoftware src/main.cpp
src/main_windows.h
src/main_windows.cpp
src/main_windows_layout.h
src/main_windows_layout.cpp)
# 添加模块
target_link_libraries(uChangAnSoftware
Qt5::Core
Qt5::Gui
Qt5::Widgets
)
if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
set(DEBUG_SUFFIX)
if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
set(DEBUG_SUFFIX "d")
endif ()
set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
endif ()
endif ()
if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
endif ()
foreach (QT_LIB Core Gui Widgets)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/bin/Qt5${QT_LIB}${DEBUG_SUFFIX}.dll"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>")
endforeach (QT_LIB)
endif ()