0
votes

Qt is installed by default in Fedora 12. Tried the hello world program:

 #include </usr/include/QtGui/QApplication>
 #include </usr/include/QtGui/QPushButton>

 int main(int argc, char *argv[])
 {
   QApplication app(argc, argv);
   QPushButton hello("Hello world!");
   hello.show();
   return app.exec();
}

With normal compilation g++ -o helloWorld helloWorld.cpp, it shows:

/tmp/ccfEv7zR.o: In function main': helloWorld.cpp:(.text+0x2a): undefined reference to QApplication::QApplication(int&, char**, int)' helloWorld.cpp:(.text+0x53): undefined reference to QPushButton::QPushButton(QString const&, QWidget*)' helloWorld.cpp:(.text+0x8b): undefined reference toQApplication::exec()' helloWorld.cpp:(.text+0x99): undefined reference to QPushButton::~QPushButton()' helloWorld.cpp:(.text+0xac): undefined reference to QPushButton::~QPushButton()' helloWorld.cpp:(.text+0xc0): undefined reference to QApplication::~QApplication()' helloWorld.cpp:(.text+0xdc): undefined reference to QApplication::~QApplication()' /tmp/ccfEv7zR.o: In function QString::QString(char const*)': helloWorld.cpp:(.text._ZN7QStringC1EPKc[QString::QString(char const*)]+0x1d): undefined reference to QString::fromAscii_helper(char const*, int)' /tmp/ccfEv7zR.o: In function QString::~QString()': helloWorld.cpp:(.text._ZN7QStringD1Ev[QString::~QString()]+0x2d): undefined reference to QString::free(QString::Data*)' collect2: ld returned 1 exit status bash: ./helloWorld: No such file or directory

and with

qmake -project;qmake;make

it shows:

/usr/lib64/qt-3.3/bin/moc /usr/include/QtGui/qapplication.h -o /usr/include/QtGui/moc_qapplication.cpp moc: Cannot create /usr/include/QtGui/moc_qapplication.cpp make: * [/usr/include/QtGui/moc_qapplication.cpp] Error 1

Any idea how to get it to work?

UPDATE:
My cpp was already in a non-read-only directory. Now I changed the header to

#include <QApplication>
#include <QPushButton>

and it shows this error:

g++ -c -pipe -Wall -W -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -DQT_NO_DEBUG -I/usr/lib64/qt-3.3/mkspecs/default -I. -I. -I/usr/lib64/qt-3.3/include -o helloWorld.o helloWorld.cpp
helloWorld.cpp:1:25: error:
QApplication: No such file or
directory helloWorld.cpp:2:24: error:
QPushButton: No such file or directory
helloWorld.cpp: In function ‘int
main(int, char*)’: helloWorld.cpp:7:
error: ‘QApplication’ was not declared
in this scope helloWorld.cpp:7: error:
expected ‘;’ before ‘app’
helloWorld.cpp:8: error: ‘QPushButton’
was not declared in this scope
helloWorld.cpp:8: error: expected ‘;’
before ‘hello’ helloWorld.cpp:9:
error: ‘hello’ was not declared in
this scope helloWorld.cpp:10: error:
‘app’ was not declared in this scope
helloWorld.cpp: At global scope:
helloWorld.cpp:5: warning: unused
parameter ‘argc’ helloWorld.cpp:5:
warning: unused parameter ‘argv’ make:
*
* [helloWorld.o] Error 1

2
Show us your PATH and QDIR environment variables. Did you do a "custom" qt install or compiled your own at one stage?Derick Schoonbee
@Derick: Are you the downvoter? I just received the "Popular Question" badge for this question. I didn't do a custom qt installed. In the very first line of my post I had mentioned that it was already installed by default in Fedora. First time I'm working with QT. How would I know about QDIR?Nav
Nope wasn't me :) I suggest that you update your OS and Qt. If not try to search for the Fedora packages that have the Qt documentation and example code. There you'll see some samples with code that compiles with Qt3. (Is there a reason why your are on an "old" Fedora? Maybe install a new box? Tried a virtual machine such as VirtualBox or even Vagrant?)Derick Schoonbee
Thanks. Am using Fedora 16 now :)Nav

2 Answers

3
votes

The problem is that /usr/include/QtGui/ is "protected". (You do not have write permissions to that directory)

Move your helloworld.cpp to a directory that you have write access to. For example /home/yourusername/helloworld/helloworld.cpp

Change:

#include </usr/include/QtGui/QApplication>
#include </usr/include/QtGui/QPushButton>

To:

#include <QApplication>
#include <QPushButton>

Then try:

qmake -project
qmake
make

I suggest you read a bit up on using qmake. You will then understand why you had the first error: qmake helps you to get the includes and right libraries step by creating a Makefile for you.

Edit:

Another thing to check is to ensure qmake is in your path and optionally if the QTDIR environment variable is set.

0
votes

From your compile log it's clear that you're using Qt 3.3.

This explains why you still can't get it to work. The CamelCase-style headers, like QPushButton, were introduced in Qt 4.0.

Most likely you've used Qt 3.3 by accident. Make sure that you run the qmake from Qt 4. On many Linux distributions this is installed as qmake-qt4.