qt5未定义引用'QApplication :: QApplication(int&,char **,int)'

时间:2018-02-12 12:22:22

标签: c++ qt5 qapplication

我正在尝试运行一个简单的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包含qt5目录的正确包含路径,其中包含QtWidgets / QApplication,QApplication文件只包含qapplication.h头文件,其中包含实际的类QApplication

1 个答案:

答案 0 :(得分:4)

教程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; DR;

对于@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对其进行编译,因为在后者中,小部件已移至称为小部件的新子模块中。