Getting Qt to work in Fedora 12
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 toQPushButton::QPushButton(QString const&, QWidget*)' helloWorld.cpp:(.text+0x8b): undefined reference to
QApplication::exec()' helloWorld.cpp:(.text+0x99): undefined reference toQPushButton::~QPushButton()' helloWorld.cpp:(.text+0xac): undefined reference to
QPushButton::~QPushButton()' helloWorld.cpp:(.text+0xc0): undefined reference toQApplication::~QApplication()' helloWorld.cpp:(.text+0xdc): undefined reference to
QApplication::~QApplication()' /tmp/ccfEv7zR.o: In functionQString::QString(char const*)': helloWorld.cpp:(.text._ZN7QStringC1EPKc[QString::QString(char const*)]+0x1d): undefined reference to
QString::fromAscii_helper(char const*开发者_Go百科, int)' /tmp/ccfEv7zR.o: In functionQString::~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
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.
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
.
精彩评论