c++qtvisual-c++cmakeclion

Qt libraries not recognized in CLion using CMake with Qt6


I am trying to set up a Qt6 project in CLion using CMake.

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)

main.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 builds without issues, but when I open the project in CLion, it cannot find the Qt libraries. Here's the error:

fatal error C1083: Cannot open include file: 'QApplication': No such file or directory

At the moment, the env variable QT6_MSVC is set to <path to qt>/Qt/6.8.1/msvc2022_64, but I have also tried I have also tried setting it to /bin, /lib, /lib/cmake, and /lib/cmake/Qt6

This is the compiler that I am using: compiler

Does anyone know what I am doing wrong?


Solution

  • I solved it. If QT6_MSVC points to <path to qt>/Qt/6.8.1/msvc2022_64 and then you add target_include_directories(ChessCMake PRIVATE ${Qt6_DIR}/include) to CMakeLists.txt, it works