4

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?

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
TMGunter
  • 137
  • 1
  • 4
  • 12
  • 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. – TMGunter Feb 02 '13 at 22:54
  • Also use the include like this: _#include _ and so on, instead of: #include it is easier (you wont need pre-processor directives for includes) to use Qt4 as target this way. – Zlatomir Feb 03 '13 at 10:17
  • well when I was trying to #include 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. – TMGunter Feb 03 '13 at 10:35

1 Answers1

6

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.

Reza Ebrahimi
  • 3,623
  • 2
  • 27
  • 41