我正在尝试安装和创建第一个 Qt 项目。我将 CLion 与 CMake 和 Qt 6 一起使用。我设置了 CMake:
cmake_minimum_required(VERSION 3.17)
project(QSnake)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(QT_VERSION 6)
set(REQUIRED_LIBS Core Gui Widgets)
set(REQUIRED_LIBS_QUALIFIED Qt6::Core Qt6::Gui Qt6::Widgets)
set(CMAKE_PREFIX_PATH D:/Qt/6.0.2/mingw81_64/lib/cmake)
add_executable(${PROJECT_NAME} main.cpp)
if(NOT CMAKE_PREFIX_PATH)
message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it "
"(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)")
endif()
find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED})
if(WIN32)
set(DEBUG_SUFFIX)
if (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 ${REQUIRED_LIBS})
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}${DEBUG_SUFFIX}.dll"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>")
endforeach(QT_LIB)
endif()
实际上,这就是创建 CLion 项目时的生成方式。我只设置了路径:D:/Qt/6.0.2/mingw81_64/lib/cmake
以防万一,我还将添加 main.c 代码。它也是由 CLion 生成的。
#include <QApplication>
#include <QPushButton>
int
main(int argc,
char *argv[])
{
QApplication a(argc, argv);
QPushButton button("Hello world!", nullptr);
button.resize(200, 100);
button.show();
return QApplication::exec();
}
我没有看到 CMake 有任何问题,但是当我尝试运行项目时出现错误:
Error copying file "D:/Qt/6.0.2/mingw81_64/lib/cmake/../../bin/Qt6Cored.dll" to "D:/QSnake/cmake-build-debug".
mingw32-make[3]: *** [CMakeFiles\QSnake.dir\build.make:126: QSnake.exe] Error 1
mingw32-make[3]: *** Deleting file 'QSnake.exe'
mingw32-make[2]: *** [CMakeFiles\Makefile2:96: CMakeFiles/QSnake.dir/all] Error 2
mingw32-make[1]: *** [CMakeFiles\Makefile2:103: CMakeFiles/QSnake.dir/rule] Error 2
mingw32-make: *** [Makefile:137: QSnake] Error 2
这是什么错误,我该如何解决?我知道构建没有问题 - 只是复制错误。并且应该将复制的 dll 称为“Cored”而不是“Core”吗?
感谢您的关注!如果您需要任何其他信息,请告诉我 - 我会尽力澄清问题。