3

I’m trying to install and create the first Qt project. I am using CLion with CMake and Qt 6. I set up 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()

Actually, this is how it was generated when the CLion project was created. I only set the path: D:/Qt/6.0.2/mingw81_64/lib/cmake

Just in case, I will also add the main.c code. It is also generated by 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();
}

I don’t see any problems with CMake, but when I try to run the project there is an error:

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

What is this error and how do I fix it? I understand that there is no problem with build - only a copying error. And should copied dll be called "Cored" and not "Core"?

Thank you for your attention! If you need any additional information, please let me know - I will try to clarify the question.

3 Answers3

4

Problem solved. Whether it is useful to anyone, but the CMake automatically generated by CLion contains rows that cause an error in debug mode (adds 'd' suffix to all dll files):

if (CMAKE_BUILD_TYPE MATCHES "Debug")
    set(DEBUG_SUFFIX "d")
endif()

Deleting those lines saved me :)

1

The real answer to this is that you are using a CMake profile defined as "debug", while you have not installed a debug version of Qt.

To solve, and not patch, if you don't want to download the debug DLLs, then create a CMake profile for "release".

Luc Morin
  • 5,302
  • 20
  • 39
0

I had the same problem and none of the answers here solved the problem. Here is how I could solved it for myself:

TL;DR:

  1. set prefix-path when creating project
  2. copy missing Qt6Cored.dll into bin-folder

When creating the new project in Clion be sure to set the correct prefix-path: setting prefix path for project

You can see in your error message that /bin/Qt6Cored.dll gets added to the prefix path automatically, so you don't have to select the bin folder when setting the prefix-path.

After this I still got an error message (but path was displayed correctly without any "/../.."). enter image description here

I then realized that there was no Qt6Cored.dll in my bin-folder: enter image description here

So I simply copied the missing file from C:\Qt\6.1.3\msvc2019_64\bin into C:\Qt\6.1.3\mingw81_64\bin

And then it worked!

If you don't have the msvc2019_64 folder you can install it by modifying Qt via app-settings: enter image description here