tags:

views:

88

answers:

5
#include <QtCore/QCoreApplication>
#include <QtGui/QLabel>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QLabel label("Hello world");
    label.show();
    return a.exec();
}

I am using QTCreator, I installed QT 4.7 on windows. Now every time I compile I get this error:

:: error: collect2: ld returned 1 exit status
With mingw32 giving me the following
tmp/obj/debug_shared/main.o: In function `main': 
E:\Qt\2010.04\qt\QTHello-build-desktop/../QTHello/main.cpp:6: undefined reference to `_imp___ZN6QLabelC1ERK7QStringP7QWidget6QFlagsIN2Qt10WindowTypeEE' 
E:\Qt\2010.04\qt\QTHello-build-desktop/../QTHello/main.cpp:8: undefined reference to `_imp___ZN6QLabelD1Ev' 
E:\Qt\2010.04\qt\QTHello-build-desktop/../QTHello/main.cpp:8: undefined reference to `_imp___ZN6QLabelD1Ev' 
collect2: ld returned 1 exit status 
mingw32-make[1]: *** [debug\QTHello.exe] Error 1 
mingw32-make: *** [debug-all] Error 2 
The process "E:/Qt/2010.04/mingw/bin/mingw32-make.exe" exited with code %2.
Error while building project QTHello (target: Desktop)
When executing build step 'Make'

Whats the problem??

+2  A: 

Your project file is probably not including the required stuff. Do you have either

TEMPLATE = app

or

QT += gui

in your .pro file?

Matti Virkkunen
I got QT -= gui
Wajih
@Wajih: Well, that'd be your problem. That's telling the linker not to include GUI stuff, which you kind of need to use `QLabel` and friends.
Matti Virkkunen
No luck, still not working after QT += gui
Wajih
gui is default option, no need to add core nor gui, so its safe to remove that line.
Tuminoid
+1  A: 

Are you linking with QtGui? If you don't, do it.

Matias Valdenegro
How is that done?
Wajih
See Matt's answer, putting QT += gui in your pro file.
Matias Valdenegro
+3  A: 

Create a Qt GUI project and olny then write what you have written.

Narek
This means you MUST build a GUI application to show up a label??? NO support in Console mode?
Wajih
@Wajih: Where do you think a label would appear in "console mode" anyways?
Matti Virkkunen
I guess you are right
Wajih
A: 

You want to create a Hello World app with QT? Really simple:

  1. Create a new GUI app into QtCreator
  2. Open the ui file and add a label using the QtDesigner embedded into QtCreator
  3. Compile and launch

You have your Hello World application done with Qt

Then if you want to know how to write a QTGui application, just open files in project to see what is written in them.

Patrice Bernassola
This means Hello World should be like HELLO WORLD in GUI only, wont show you the label unless you build a GUI project.
Wajih
+1  A: 

Also, for UI applications you must use QApplication. QCoreApplication is for CLI-only tools.

Frank
Yup, QCoreApp isn't enough, so this is probably your last, final issue.
Tuminoid