Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

qt5 undefined reference to 'QApplication::QApplication(int&, char**, int)'

I'm trying to get a simple hello world example running, and already needed some time to figure out what includes to use Now I verified the include paths, the QApplication should actually be there, but it throws the above error. For clarity my code:

#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();
}

I tried compiling using first qmake -project, then qmake and finally make and then got the following errors:

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()

The Makefile created by qmake contains the correct include path to the qt5 directory which contains the QtWidgets/QApplication, the QApplication file just includes the qapplication.h header that contains the actual class QApplication.

like image 436
Marcel H. Avatar asked Feb 12 '18 12:02

Marcel H.


1 Answers

The tutorial https://wiki.qt.io/Qt_for_Beginners is fully updated so you must modify it. Change to:

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

TL; DR;

In the case of @jww it has an error in the following line of the .pro:

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

The error is caused because greaterThan(QT_MAJOR_VERSION, 5) verifies that the major version of Qt is greater than 5 to add sub-modules, but the latest version of Qt is 5.13.2 is not greater than 5, so it is not linked the modules causing the error shown.

In the tutorial greaterThan(QT_MAJOR_VERSION, 4): QT += widgets is used to support .pro so that it can be compiled for Qt4 and Qt5 since in the latter the widgets moved to a new sub-module called widgets.

like image 73
eyllanesc Avatar answered Nov 09 '22 00:11

eyllanesc