7

我正在尝试运行一个简单的 hello world 示例,并且已经需要一些时间来弄清楚要使用的包含现在我验证了包含路径,QApplication 实际上应该在那里,但它会引发上述错误。为清楚起见,我的代码:

#include <QtWidgets/QApplication>
#include <QtWidgets/QPushButton>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QPushButton *button = new QPushButton("Hello world!");
    button->show();
    return app.exec();
}

我尝试使用第一个qmake -project进行编译,然后是qmake,最后是 make,然后出现以下错误:

qt_hello_world.o: In function 'main':  
undefined reference to QApplication::QApplication(int&, char**, int)  
qt_hello_world.cpp: undefined reference to QPushButton::QPushButton(QString const&, QWidget*)
qt_hello_world.cpp: undefined reference to QWidget::show()  
qt_hello_world.cpp: undefined reference to QApplication::exec()  
qt_hello_world.cpp: undefined reference to QApplication::~QApplication()
qt_hello_world.cpp: undefined reference to QApplication::~QApplication()

由 qmake 创建的 Makefile 包含到包含 QtWidgets/QApplication 的 qt5 目录的正确包含路径,QApplication 文件仅包含包含实际类 QApplication 的 qapplication.h 头文件。

4

1 回答 1

8

教程https://wiki.qt.io/Qt_for_Beginners已完全更新,因此您必须对其进行修改。改成:

TEMPLATE = app
TARGET = callboot-ui.exe
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
HEADERS +=
SOURCES += main.cpp

TL; 博士;

@jww的情况下,.pro 的以下行中有错误:

greaterThan(QT_MAJOR_VERSION, 5): QT += core gui widgets

该错误是因为greaterThan(QT_MAJOR_VERSION, 5)验证Qt的主版本大于5添加子模块,但Qt的最新版本是5 .13.2不大于5,所以没有链接导致显示错误的模块。

在本教程greaterThan(QT_MAJOR_VERSION, 4): QT += widgets中用于支持 .pro 以便可以为 Qt4 和 Qt5 编译它,因为在后者中,小部件移动到一个称为小部件的新子模块。

于 2019-12-09T18:14:26.610 回答