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

1

9

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.

Mangrove answered 12/2, 2018 at 12:22 Comment(10)
Undefined reference to ... is a linker error, hence it is unrelated to includes, or include directories.Huertas
but where can I start to resolve that error? the Makefile includes it as /usr/include/x86_64-linux-gnu/qt5, in the cpp file I included the header as QtWidgets/QApplication, the full path to the QApplication file is /usr/include/x86_64-linux-gnu/qt5/QtWidgets/QApplication, shouldn't that actually fit? Or am I missunderstanding something?Mangrove
As I already mentioned: since it is linker error, it is unrealted to includes. Did you read this, during your research prior to asking this question: #12574316 ?Huertas
no, sorry I didn't, sorry I'm quite new to c++, it seems that the implementation of QApplication is missing, right? But where or how can I find it? Doesn't seem to be in the same folder as the header file, grep would have told me soMangrove
Doesn't seem to be in the same folder as the header file It should not be. With that said since you are using qmake you probably have a bug in your .pro file.Clute
@marcoPolio Did you link the QT library files into your application? Consider reading the above link, or learning from a good C++ book.Huertas
yes, seems like it, make does: g++ -m64 -Wl,-O1 -o helloworld qt_hello_world.o -L/usr/X11R6/lib64 -lQt5Gui -lQt5Core -lGL -lpthreadMangrove
Hi Marco (or future googler), QApplication is defined in the QtWidgets library, not QtCore or QtGUI. You need to add QT += widgets to your .pro file.Wellknown
@jww The tutorial you point out is compatible with Qt5, could you give more details of your problem?Quesenberry
@Quesenberry - Qt 5 link errors on Ubuntu 18. Here are the relevant files: Qt 5 PRO file and Qt 5 Makefile. The Makefile was created from the PRO file using qmake.Silage
Q
10

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.

Quesenberry answered 9/12, 2019 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.