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.

jww
  • 97,681
  • 90
  • 411
  • 885
Marcel H.
  • 197
  • 1
  • 2
  • 14
  • 7
    `Undefined reference to ...` is a linker error, hence it is **unrelated** to includes, or include directories. – Algirdas Preidžius Feb 12 '18 at 12:25
  • 1
    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? – Marcel H. Feb 12 '18 at 12:32
  • 1
    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: https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix ? – Algirdas Preidžius Feb 12 '18 at 12:39
  • 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 so – Marcel H. Feb 12 '18 at 13:32
  • 1
    ***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. – drescherjm Feb 12 '18 at 13:37
  • 1
    @marcoPolio Did you link the QT library files into your application? Consider reading the above link, or learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Feb 12 '18 at 16:08
  • yes, seems like it, make does: g++ -m64 -Wl,-O1 -o helloworld qt_hello_world.o -L/usr/X11R6/lib64 -lQt5Gui -lQt5Core -lGL -lpthread – Marcel H. Feb 12 '18 at 18:38
  • 4
    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. – Colin Mar 31 '18 at 04:59
  • @jww The tutorial you point out is compatible with Qt5, could you give more details of your problem? – eyllanesc Dec 09 '19 at 17:53
  • @eyllanesc - [Qt 5 link errors on Ubuntu 18](https://pastebin.com/XQNwVLCD). Here are the relevant files: [Qt 5 PRO file](https://pastebin.com/egZPvNg4) and [Qt 5 Makefile](https://pastebin.com/Cq7D6PKE). The Makefile was created from the PRO file using qmake. – jww Dec 09 '19 at 17:57

1 Answers1

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241