Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't compile project using CLion and CMake
QtWS25 Last Chance

Can't compile project using CLion and CMake

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt6clion-idecmakeerror
37 Posts 6 Posters 10.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mingvv
    wrote on 17 Mar 2021, 12:19 last edited by mingvv
    #1

    Hello, Qt community!
    I’m new to programming, especially in Qt. So please don’t swear if I write anything stupid.
    Now 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()
    #D:\Qt\6.0.2\mingw81_64\lib\cmake
    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
    

    Can you please tell me what this is about? I guess I haven’t set something up or pointed out a way?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 17 Mar 2021, 19:36 last edited by
      #2

      Hi and welcome to devnet,

      @mingvv said in Can't compile project using CLion and CMake:

      Qt6Cored.dll

      Where do you have that file in your Qt 6 installation ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mingvv
        wrote on 17 Mar 2021, 20:58 last edited by
        #3

        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 :)

        C 1 Reply Last reply 18 Mar 2021, 05:55
        0
        • N Offline
          N Offline
          nagesh
          wrote on 18 Mar 2021, 00:27 last edited by
          #4

          @mingvv during debug it will link with debug version of the dll.
          eg:QtCored.dll

          M 1 Reply Last reply 18 Mar 2021, 06:54
          0
          • M mingvv
            17 Mar 2021, 20:58

            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 :)

            C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 18 Mar 2021, 05:55 last edited by
            #5

            @mingvv said in Can't compile project using CLion and CMake:

            Deleting those lines saved me :)

            I don't see why this would help anything.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • N nagesh
              18 Mar 2021, 00:27

              @mingvv during debug it will link with debug version of the dll.
              eg:QtCored.dll

              M Offline
              M Offline
              mingvv
              wrote on 18 Mar 2021, 06:54 last edited by
              #6

              @nagesh Maybe I don't have debug dll?

              1 Reply Last reply
              0
              • C Online
                C Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 18 Mar 2021, 06:57 last edited by Christian Ehrlicher
                #7

                Since you can't mix (msvc) / should not (mingw) debug and release libs it's dangerous to use Qt release dlls with a debug build of your program.
                And this strang install stuff is not needed at all - use windeployqt

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                M 1 Reply Last reply 18 Mar 2021, 08:38
                1
                • C Christian Ehrlicher
                  18 Mar 2021, 06:57

                  Since you can't mix (msvc) / should not (mingw) debug and release libs it's dangerous to use Qt release dlls with a debug build of your program.
                  And this strang install stuff is not needed at all - use windeployqt

                  M Offline
                  M Offline
                  mingvv
                  wrote on 18 Mar 2021, 08:38 last edited by
                  #8

                  @Christian-Ehrlicher said in Can't compile project using CLion and CMake:

                  Since you can't mix (msvc) / should not (mingw) debug and release libs it's dangerous to use Qt release dlls with a debug build of your program.

                  Where can I get debug dll files? I reinstalled Qt, but it didn’t help. Are they at another address?

                  @Christian-Ehrlicher said in Can't compile project using CLion and CMake:

                  And this strang install stuff is not needed at all - use windeployqt

                  What does it do? This utility helps configure Qt for Windows?

                  And then there’s the question. Can I run Qt project (CMake) using Visual Studio?

                  J 1 Reply Last reply 18 Mar 2021, 08:40
                  0
                  • M mingvv
                    18 Mar 2021, 08:38

                    @Christian-Ehrlicher said in Can't compile project using CLion and CMake:

                    Since you can't mix (msvc) / should not (mingw) debug and release libs it's dangerous to use Qt release dlls with a debug build of your program.

                    Where can I get debug dll files? I reinstalled Qt, but it didn’t help. Are they at another address?

                    @Christian-Ehrlicher said in Can't compile project using CLion and CMake:

                    And this strang install stuff is not needed at all - use windeployqt

                    What does it do? This utility helps configure Qt for Windows?

                    And then there’s the question. Can I run Qt project (CMake) using Visual Studio?

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 18 Mar 2021, 08:40 last edited by
                    #9

                    @mingvv said in Can't compile project using CLion and CMake:

                    What does it do?

                    It deploys Qt applications. See https://doc.qt.io/qt-5/windows-deployment.html

                    Qt debug libs are installed together with release libs if you use Qt Online Installer/Maintenance Tool.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    M 1 Reply Last reply 18 Mar 2021, 08:52
                    0
                    • J jsulm
                      18 Mar 2021, 08:40

                      @mingvv said in Can't compile project using CLion and CMake:

                      What does it do?

                      It deploys Qt applications. See https://doc.qt.io/qt-5/windows-deployment.html

                      Qt debug libs are installed together with release libs if you use Qt Online Installer/Maintenance Tool.

                      M Offline
                      M Offline
                      mingvv
                      wrote on 18 Mar 2021, 08:52 last edited by
                      #10

                      @jsulm Yes, but using QtCreator I can simply build and launch the project during the development phase. With other IDE to build Qt I cannot. So I’m not sure if this windeployqt.exe is what I need. Maybe I just don’t understand why I need to... But my current goal is simply to build a project using at least Visual Studio.

                      J 1 Reply Last reply 18 Mar 2021, 08:55
                      0
                      • M mingvv
                        18 Mar 2021, 08:52

                        @jsulm Yes, but using QtCreator I can simply build and launch the project during the development phase. With other IDE to build Qt I cannot. So I’m not sure if this windeployqt.exe is what I need. Maybe I just don’t understand why I need to... But my current goal is simply to build a project using at least Visual Studio.

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 18 Mar 2021, 08:55 last edited by
                        #11

                        @mingvv said in Can't compile project using CLion and CMake:

                        With other IDE to build Qt I cannot

                        OK, then this is indeed another issue.
                        If you're using Visual Studio then this line is wrong:

                        set(CMAKE_PREFIX_PATH D:/Qt/6.0.2/mingw81_64/lib/cmake)
                        

                        It should point to Qt for Visual Studio, not for MinGW.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        M 1 Reply Last reply 18 Mar 2021, 09:09
                        0
                        • J jsulm
                          18 Mar 2021, 08:55

                          @mingvv said in Can't compile project using CLion and CMake:

                          With other IDE to build Qt I cannot

                          OK, then this is indeed another issue.
                          If you're using Visual Studio then this line is wrong:

                          set(CMAKE_PREFIX_PATH D:/Qt/6.0.2/mingw81_64/lib/cmake)
                          

                          It should point to Qt for Visual Studio, not for MinGW.

                          M Offline
                          M Offline
                          mingvv
                          wrote on 18 Mar 2021, 09:09 last edited by mingvv
                          #12

                          @jsulm said in Can't compile project using CLion and CMake:

                          @mingvv said in Can't compile project using CLion and CMake:

                          With other IDE to build Qt I cannot

                          OK, then this is indeed another issue.
                          If you're using Visual Studio then this line is wrong:

                          set(CMAKE_PREFIX_PATH D:/Qt/6.0.2/mingw81_64/lib/cmake)
                          

                          It should point to Qt for Visual Studio, not for MinGW.

                          I only have this folder. Using MaintenanceTool.exe I also don't see separate components for MSVC.

                          I used CMake GUI app to generate projects and solution (.sln), but while trying to compile I have 18 errors in library files. Is that why I have MinGW in CMake?

                          J 1 Reply Last reply 18 Mar 2021, 09:11
                          0
                          • M mingvv
                            18 Mar 2021, 09:09

                            @jsulm said in Can't compile project using CLion and CMake:

                            @mingvv said in Can't compile project using CLion and CMake:

                            With other IDE to build Qt I cannot

                            OK, then this is indeed another issue.
                            If you're using Visual Studio then this line is wrong:

                            set(CMAKE_PREFIX_PATH D:/Qt/6.0.2/mingw81_64/lib/cmake)
                            

                            It should point to Qt for Visual Studio, not for MinGW.

                            I only have this folder. Using MaintenanceTool.exe I also don't see separate components for MSVC.

                            I used CMake GUI app to generate projects and solution (.sln), but while trying to compile I have 18 errors in library files. Is that why I have MinGW in CMake?

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 18 Mar 2021, 09:11 last edited by
                            #13

                            @mingvv said in Can't compile project using CLion and CMake:

                            Using MaintenanceTool.exe I also don't see separate components for MSVC

                            You should. You could try to reinstall Qt.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            M 1 Reply Last reply 18 Mar 2021, 09:26
                            0
                            • J jsulm
                              18 Mar 2021, 09:11

                              @mingvv said in Can't compile project using CLion and CMake:

                              Using MaintenanceTool.exe I also don't see separate components for MSVC

                              You should. You could try to reinstall Qt.

                              M Offline
                              M Offline
                              mingvv
                              wrote on 18 Mar 2021, 09:26 last edited by
                              #14

                              @jsulm
                              Indeed, it has now been possible to install the MSVC version. I have upgraded CMake, but this has not corrected the bugs in the project build:

                              2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QString::~QString(void)" (__imp_??1QString@@QEAA@XZ) referenced in function main
                              2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QString::QString(char const *)" (__imp_??0QString@@QEAA@PEBD@Z) referenced in function main
                              2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QApplication::QApplication(int &,char * *,int)" (__imp_??0QApplication@@QEAA@AEAHPEAPEADH@Z) referenced in function main
                              2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl QApplication::~QApplication(void)" (__imp_??1QApplication@@UEAA@XZ) referenced in function main
                              2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static int __cdecl QApplication::exec(void)" (__imp_?exec@QApplication@@SAHXZ) referenced in function main
                              2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl QWidget::show(void)" (__imp_?show@QWidget@@QEAAXXZ) referenced in function main
                              2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl QWidget::resize(int,int)" (__imp_?resize@QWidget@@QEAAXHH@Z) referenced in function main
                              2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QPushButton::QPushButton(class QString const &,class QWidget *)" (__imp_??0QPushButton@@QEAA@AEBVQString@@PEAVQWidget@@@Z) referenced in function main
                              2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl QPushButton::~QPushButton(void)" (__imp_??1QPushButton@@UEAA@XZ) referenced in function main
                              2>main.obj : error LNK2001: unresolved external symbol "public: virtual __cdecl QApplication::~QApplication(void)" (??1QApplication@@UEAA@XZ)
                              2>main.obj : error LNK2001: unresolved external symbol "public: virtual __cdecl QPushButton::~QPushButton(void)" (??1QPushButton@@UEAA@XZ)
                              2>main.obj : error LNK2001: unresolved external symbol "public: __cdecl QString::~QString(void)" (??1QString@@QEAA@XZ)
                              2>D:\QSnake\RelWithDebInfo\QSnake.exe : fatal error LNK1120: 12 unresolved externals
                              
                              J 1 Reply Last reply 18 Mar 2021, 09:32
                              0
                              • M mingvv
                                18 Mar 2021, 09:26

                                @jsulm
                                Indeed, it has now been possible to install the MSVC version. I have upgraded CMake, but this has not corrected the bugs in the project build:

                                2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QString::~QString(void)" (__imp_??1QString@@QEAA@XZ) referenced in function main
                                2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QString::QString(char const *)" (__imp_??0QString@@QEAA@PEBD@Z) referenced in function main
                                2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QApplication::QApplication(int &,char * *,int)" (__imp_??0QApplication@@QEAA@AEAHPEAPEADH@Z) referenced in function main
                                2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl QApplication::~QApplication(void)" (__imp_??1QApplication@@UEAA@XZ) referenced in function main
                                2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static int __cdecl QApplication::exec(void)" (__imp_?exec@QApplication@@SAHXZ) referenced in function main
                                2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl QWidget::show(void)" (__imp_?show@QWidget@@QEAAXXZ) referenced in function main
                                2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl QWidget::resize(int,int)" (__imp_?resize@QWidget@@QEAAXHH@Z) referenced in function main
                                2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QPushButton::QPushButton(class QString const &,class QWidget *)" (__imp_??0QPushButton@@QEAA@AEBVQString@@PEAVQWidget@@@Z) referenced in function main
                                2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl QPushButton::~QPushButton(void)" (__imp_??1QPushButton@@UEAA@XZ) referenced in function main
                                2>main.obj : error LNK2001: unresolved external symbol "public: virtual __cdecl QApplication::~QApplication(void)" (??1QApplication@@UEAA@XZ)
                                2>main.obj : error LNK2001: unresolved external symbol "public: virtual __cdecl QPushButton::~QPushButton(void)" (??1QPushButton@@UEAA@XZ)
                                2>main.obj : error LNK2001: unresolved external symbol "public: __cdecl QString::~QString(void)" (??1QString@@QEAA@XZ)
                                2>D:\QSnake\RelWithDebInfo\QSnake.exe : fatal error LNK1120: 12 unresolved externals
                                
                                J Offline
                                J Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 18 Mar 2021, 09:32 last edited by
                                #15

                                @mingvv Did you try a complete rebuild?

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                M 1 Reply Last reply 18 Mar 2021, 09:48
                                0
                                • J jsulm
                                  18 Mar 2021, 09:32

                                  @mingvv Did you try a complete rebuild?

                                  M Offline
                                  M Offline
                                  mingvv
                                  wrote on 18 Mar 2021, 09:48 last edited by
                                  #16

                                  @jsulm Yes, I cleaned solution and rebuilt. Maybe I generated it wrong with CMake? Is there any build guide for Visual Studio or any hidden features?

                                  1 Reply Last reply
                                  0
                                  • C Online
                                    C Online
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on 18 Mar 2021, 09:48 last edited by
                                    #17

                                    And make sure that the msvc libs are used during linking.

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      mingvv
                                      wrote on 18 Mar 2021, 09:58 last edited by
                                      #18

                                      Maybe I should add project additional include directories?

                                      @Christian-Ehrlicher said in Can't compile project using CLion and CMake:

                                      And make sure that the msvc libs are used during linking.

                                      How can I check it? I guess I have default settings, because I didn't change them.

                                      P.S. Maybe you know, after how many messages can I post more than once in 10 minutes?

                                      1 Reply Last reply
                                      1
                                      • C Online
                                        C Online
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on 18 Mar 2021, 10:12 last edited by
                                        #19

                                        @mingvv said in Can't compile project using CLion and CMake:

                                        How can I check it?

                                        Since you've a msvc project take a look in the linker options. Or use cmake-gui to see the CMake values.

                                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                        Visit the Qt Academy at https://academy.qt.io/catalog

                                        M 1 Reply Last reply 18 Mar 2021, 10:14
                                        0
                                        • C Christian Ehrlicher
                                          18 Mar 2021, 10:12

                                          @mingvv said in Can't compile project using CLion and CMake:

                                          How can I check it?

                                          Since you've a msvc project take a look in the linker options. Or use cmake-gui to see the CMake values.

                                          M Offline
                                          M Offline
                                          mingvv
                                          wrote on 18 Mar 2021, 10:14 last edited by
                                          #20

                                          @Christian-Ehrlicher If I correctly understood you, I use MSVC linker:
                                          CMAKE_LINKER - C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/link.exe

                                          1 Reply Last reply
                                          0

                                          3/37

                                          17 Mar 2021, 20:58

                                          34 unread
                                          • Login

                                          • Login or register to search.
                                          3 out of 37
                                          • First post
                                            3/37
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved