我正在尝试使用 CMake 在 CLion 中设置 Qt6 项目。
CMakeLists.txt
cmake_minimum_required(VERSION 3.29)
project(ChessCMake)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
if(NOT DEFINED ENV{QT6_MSVC})
message(FATAL_ERROR "Environment variable QT6_MSVC is not set. Please set it to the Qt6 directory.")
endif()
set(Qt6_DIR $ENV{QT6_MSVC})
set(CMAKE_PREFIX_PATH ${Qt6_DIR})
find_package(Qt6 COMPONENTS
Core
Gui
Widgets
REQUIRED)
add_executable(ChessCMake main.cpp)
target_link_libraries(ChessCMake PRIVATE Qt6::Core Qt6::Gui Qt6::Widgets)
主.cpp
#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 构建没有问题,但是当我在 CLion 中打开项目时,它找不到 Qt 库。错误如下:
fatal error C1083: Cannot open include file: 'QApplication': No such file or directory
目前,环境变量
QT6_MSVC
设置为<path to qt>/Qt/6.8.1/msvc2022_64
,但我也尝试过将其设置为/bin
、/lib
、/lib/cmake
和/lib/cmake/Qt6
有人知道我做错了什么吗?
我解决了。如果 QT6_MSVC 指向
<path to qt>/Qt/6.8.1/msvc2022_64
然后将 target_include_directories(ChessCMake PRIVATE ${Qt6_DIR}/include)
添加到 CMakeLists.txt,则它可以工作