PDA

View Full Version : undefined reference Error in helloworld program !



prady
15th December 2010, 11:20
Hi,

I am again new to Qt world !

I have created a Helloworld Program in Qt 4.7.0 ( 32 bit windows ), using QtCreater 2.0.1.



#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();
}


when I build this code getting the following errors,


main.cpp:11: undefined reference to `_imp___ZN6QLabelD1Ev'

collect2: ld returned 1 exit status

could anybody let me know, what I am doing wrong in this simple code ???

Thanks,
Pradeep.

high_flyer
15th December 2010, 11:36
change QCoreApplication to QApplication. - and the corresponding include as well.

prady
15th December 2010, 13:11
Hi,

Thanks for quick reply, but the solution is not working for me !
now the code is changed to



#include <QApplication>
#include <QtGui\QLabel>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QLabel label("Hello World!");
label.show();

return a.exec();
}


but i am getting the following errors:

main.cpp:1:24: error: QApplication: No such file or directory

main.cpp:6: error: variable 'QApplication a' has initializer but incomplete type

when I choolse "Qt Console Application", while creating new project, by default QtCreator includes QtCoreApplicaion. so is that wrong ?

-Pradeep-

high_flyer
15th December 2010, 14:38
#include <QtGui\QApplication>


when I choolse "Qt Console Application", while creating new project, by default QtCreator includes QtCoreApplicaion. so is that wrong ?


No, QtCreator is doing it right.
But a console application by definition has no GUI. (I am not talking about ncourses style GUI).
Yet you are initializing gui elements in your code!
So either use GUI and make a GUI application, or a console one, without GUI.
But trying to use GUI elements in non GUI application wont work.

prady
16th December 2010, 05:12
Yes I understand now, thank you.

but only adding #include <QtGui\QApplication> is not fixing my problem.
I have modified "QT -= gui" line in .pro file to "QT += gui" helps to solve those errors.

I assume this tells compiler to include gui libraries.

Thanks again,
Pradeep.

high_flyer
16th December 2010, 10:12
yes, that is correct.
In my first answer I was not aware you configured a console application.