Solved Use cmake to write qt programs in CLion, can not compile.
-
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/qtMy 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
-
You also have to add the Qt libraries to your target with TARGET_LINK_LIBRARIES: https://doc.qt.io/qt-5/cmake-manual.html
-
You also have to add the Qt libraries to your target with TARGET_LINK_LIBRARIES: https://doc.qt.io/qt-5/cmake-manual.html
-
@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})
-
There is no need to call find_package() more than once
-
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"; }
-
@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!
-
@Christian-Ehrlicher Thanks!