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. Use cmake to write qt programs in CLion, can not compile.
Forum Updated to NodeBB v4.3 + New Features

Use cmake to write qt programs in CLion, can not compile.

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 556 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.
  • S Offline
    S Offline
    senmx
    wrote on 26 Mar 2020, 06:17 last edited by senmx
    #1

    I use cmake to write qt programs in CLion, but cannot
    #include <QGuiApplication>
    only
    #include <qt/QtCore/QGuiApplication>
    and also cannot compile.
    After the qt installation, the header files are in /usr/include/qt

    My CMakeLists.txt:

    cmake_minimum_required(VERSION 3.15)
    project(cmake_test)
    
    set(CMAKE_CXX_STANDARD 14)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTOUIC ON)
    set(CMAKE_AUTORCC ON)
    set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} $ENV{QT_DIR})
    
    find_package(Qt5 COMPONENTS Core Quick Qml Sql REQUIRED)
    find_package(Qt5QuickCompiler)
    
    aux_source_directory(. SRC_LIST)
    add_subdirectory(a)
    
    qtquick_compiler_add_resources(RESOURCES qml.qrc)
    
    add_executable(cmake_test ${SRC_LIST} ${RESOURCES})
    target_link_libraries(${PROJECT_NAME} a)
    

    CMAKE_PREFIX_PATH and QT_DIR are empty.

    Project tree:

    cmake_test/
    ├── a
    │   ├── a.cpp
    │   ├── a.h
    │   └── CMakeLists.txt
    ├── CMakeLists.txt
    └── main.cpp
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 26 Mar 2020, 06:33 last edited by
      #2

      You also have to add the Qt libraries to your target with TARGET_LINK_LIBRARIES: https://doc.qt.io/qt-5/cmake-manual.html

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

      S 1 Reply Last reply 26 Mar 2020, 08:52
      2
      • C Christian Ehrlicher
        26 Mar 2020, 06:33

        You also have to add the Qt libraries to your target with TARGET_LINK_LIBRARIES: https://doc.qt.io/qt-5/cmake-manual.html

        S Offline
        S Offline
        senmx
        wrote on 26 Mar 2020, 08:52 last edited by
        #3

        @Christian-Ehrlicher Thanks, now it works.

        target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Quick Qt5::Qml Qt5::Sql a)
        

        But, if I use the qt module in the subdirectory, this problem also occurs in the subdirectory, and adding find_package to the sub CMakeLists.txt does not seem to work.

        a/CMakeLitsts.txt:

        aux_source_directory(. DIR_LIB_SRCS)
        find_package(Qt5 COMPONENTS Core Quick Qml Sql REQUIRED)
        add_library(a ${DIR_LIB_SRCS})
        
        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 26 Mar 2020, 08:55 last edited by
          #4

          There is no need to call find_package() more than once

          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
          • S Offline
            S Offline
            senmx
            wrote on 26 Mar 2020, 09:38 last edited by
            #5

            Well, this line was originally not added, but cannot include qt module, and it is ok in main.cpp.

            a.cpp:

            #include <iostream>
            #include <QDebug> // Cannot include
            #include "a.h"
            
            void a() {
                std::cout << "aaa" << std::endl;
                qDebug() << "xxxx";
            }
            
            1 Reply Last reply
            -1
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 26 Mar 2020, 09:40 last edited by
              #6

              @senmx said in Use cmake to write qt programs in CLion, can not compile.:

              Well, this line was originally not added, but cannot include qt module, and it is ok in main.cpp.

              Again: You also have to add the Qt libraries to your target with TARGET_LINK_LIBRARIES

              You don't do this for your target!

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

              S 1 Reply Last reply 26 Mar 2020, 10:00
              3
              • C Christian Ehrlicher
                26 Mar 2020, 09:40

                @senmx said in Use cmake to write qt programs in CLion, can not compile.:

                Well, this line was originally not added, but cannot include qt module, and it is ok in main.cpp.

                Again: You also have to add the Qt libraries to your target with TARGET_LINK_LIBRARIES

                You don't do this for your target!

                S Offline
                S Offline
                senmx
                wrote on 26 Mar 2020, 10:00 last edited by
                #7

                @Christian-Ehrlicher Thanks!

                1 Reply Last reply
                1

                1/7

                26 Mar 2020, 06:17

                • Login

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