Search code examples
qtmakefilelinker-errorsqmake

Qt linker errors Qmake + Makefile


I want to build a Qt project outside Qt Creator, so I'm using qmake to generate a Makefile on the following project file:

TEMPLATE = app
TARGET = test
INCLUDEPATH += . \
               /usr/include/qt5/QtWidgets/
QMAKE_CXXFLAGS += --std=c++11

# Input
SOURCES += test.cc

Which was generated also by qmake, bar the c++11 flag and the second include path. The Makefile contains link paths to the Qt library

LIBS = $(SUBLIBS) -L/usr/X11R6/lib64 -lQt5Gui -L/usr/lib/x86_64-linux-gnu -lQt5Core -lG L -lpthread

What's weird in the above is that I don't have a /usr/X11R6 folder. Instead, libQt5Gui.so is located in /usr/lib/x86_64-linux-gnu, so I'm a little puzzled where the X11R6 comes from.

Anyway, this is my linker output:

test.cc:(.text.startup+0x20): undefined reference to `QApplication::QApplication(int&, char**, int)'
test.cc:(.text.startup+0x25): undefined reference to `QApplication::exec()'
test.cc:(.text.startup+0x2f): undefined reference to `QApplication::~QApplication()'
test.cc:(.text.startup+0x43): undefined reference to `QApplication::~QApplication()'

The above is the result of building the following source:

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    return app.exec();
}

When I try to build the project file in Qt Creator, the same error appears. Am I missing libraries? Or is something configured erroneously?

(I'm on Ubuntu 14.04, and I just installed the qtcreator package from the repo's, assuming that all development libraries would be installed along with it.)


Solution

  • As stated in the docs, you need to include the widget library to use QApplication.

    Add this to your project file:

    QT += widgets
    

    If you're not going to build a GUI app, use QCoreApplication instead. It doesn't have that dependency.