Qt LNK2019 Error with basic Qt5 application

TMGunter picture TMGunter · Feb 2, 2013 · Viewed 11k times · Source

I am trying to follow a tutorial online and learn Qt5 with QtCreator 2.6.1

However, I went and attempted to write a basic application following this tutorial and I keep getting a linking error whenever I try to build the project:

#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QLabel *label = new QLabel("Hello World");
    label->show();

    return app.exec();
}

Once I click "Build" I get about 50 errors resulting similar to the following:

main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall QApplication::~QApplication(void)" (__imp_??1QApplication@@UAE@XZ) referenced in function _main
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: static int __cdecl QApplication::exec(void)" (__imp_?exec@QApplication@@SAHXZ) referenced in function _main
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall QWidget::show(void)" (__imp_?show@QWidget@@QAEXXZ) referenced in function _main

Is there a way I can fix this by linking the libraries, etc (assuming they aren't linking correctly)? If not, is there something else I can do to try and resolve this problem?

Answer

Reza Ebrahimi picture Reza Ebrahimi · Feb 3, 2013

this a linker fault that can't link object files together, first delete all the build directory, then check that this line was added to your .pro file:

QT += core gui widgets

this should work, if gets fail again, you must give more information about your compiler and their search path, plus post your .pro file content here.