cmake Qt6函数未定义

eqqqjvef  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(109)

我使用sudo apt install qt6-base-dev命令在Ubuntu上安装了Qt 6。我创建了Clion Qt 6项目并编写了简单的窗口代码:

#include <QCoreApplication>
#include <QDebug>
#include <QtWidgets/QWidget>

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    QWidget window;
    window.resize(320, 240);
    window.show();
    window.setWindowTitle(
            QCoreApplication::translate("toplevel", "Notepad")
    );
    return QCoreApplication::exec();
}

字符串
所以我试着做了一个。但是出错了

/home/usr/CLionProjects/untitled/main.cpp:8:(.text+0x58): undefined reference to `QWidget::QWidget(QWidget*, QFlags<Qt::WindowType>)'
/usr/bin/ld: /home/usr/CLionProjects/untitled/main.cpp:10:(.text+0x7a): undefined reference to `QWidget::show()'
/usr/bin/ld: /home/usr/CLionProjects/untitled/main.cpp:11:(.text+0xb2): undefined reference to `QWidget::setWindowTitle(QString const&)'
/usr/bin/ld: /home/usr/CLionProjects/untitled/main.cpp:15:(.text+0xd2): undefined reference to `QWidget::~QWidget()'
/usr/bin/ld: /home/usr/CLionProjects/untitled/main.cpp:15:(.text+0x119): undefined reference to `QWidget::~QWidget()'
/usr/bin/ld: CMakeFiles/untitled.dir/main.cpp.o: в функции «QWidget::resize(int, int)»:
/usr/include/x86_64-linux-gnu/qt6/QtWidgets/qwidget.h:881:(.text._ZN7QWidget6resizeEii[_ZN7QWidget6resizeEii]+0x48): undefined reference to `QWidget::resize(QSize const&)'
collect2: error: ld returned 1 exit status


这是CMakeLists.txt:

cmake_minimum_required(VERSION 3.26)
project(untitled)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

find_package(Qt6 COMPONENTS
  Core
  REQUIRED)

add_executable(untitled main.cpp)
target_link_libraries(untitled
  Qt::Core
)


我进入了Qt 6文件。发现,所有的头都有类,命名空间等的声明。但所有的C文件都有一个字符串-#include <A>(其中C文件是a. c,头是A. h)。
我尝试重新安装Qt 6。但没有帮助。我如何修复它?

toe95027

toe950271#

您需要将QWidget添加到您的链接库中,如下所示:
CMakeLists.txt:

find_package(Qt6 REQUIRED COMPONENTS Core Widgets)

个字符
此外,要使用QWidgets,您需要使用QApplication,而不是QCoreApplication,因为后者适用于无头(非GUI)应用程序。我建议您阅读Qt的CMake应用程序指南中有关GUI应用程序的部分。

相关问题