Qt LNK2019 Error with basic Qt5 application

11,275

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.

Share:
11,275
TMGunter
Author by

TMGunter

Updated on August 21, 2022

Comments

  • TMGunter
    TMGunter over 1 year

    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?

    • TMGunter
      TMGunter over 11 years
      I think I finally figured out what I was doing wrong. I had to add QT += widgets to my project (.pro) file. Then I read up on using qmake, ran it, and then it seemed to compile and build just fine.
    • Zlatomir
      Zlatomir over 11 years
      Also use the include like this: #include <QApplication> and so on, instead of: #include <QtWidgets/QApplication> it is easier (you wont need pre-processor directives for includes) to use Qt4 as target this way.
    • TMGunter
      TMGunter over 11 years
      well when I was trying to #include <QApplication> it was throwing me an error in the IDE, telling me that it couldn't locate the file QApplication. QWidget/WApplication it was able to find though. After changing the .pro file though, I was able to do that.
  • Zlatomir
    Zlatomir over 11 years
    It's solved already, by adding QT+= widgets in the .pro file.