在Fedora上的安裝Qt4,我一開始就用了root用戶,與Ubuntu差別很小。
#mv qt-everywhere-opensource-src-4.6.2.tar.gz /tmp
#cd /tmp
#gunzip qt-everywhere-opensource-src-4.6.2.tar.gz
#tar xvf qt-everywhere-opensource-src-4.6.2.tar
#cd qt-everywhere-opensource-src-4.6.2
#./configure
#gmake (注意此處是gmake)
#gmake install
環境變量設置略。
按照前文中的步驟在Fedora 5上安裝完Qt4.6.2後,設置好環境變量,www.linuxidc.com用qmake編譯程序hello.cpp,程序代碼如下(C++ GUI Programming With Qt 4的第一個程序):
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
return app.exec();
}
$qmake -project
$qmake hello.pro
$make
然後出現錯誤:
hello.cpp:1:24: error: QApplication: No such file or directory
hello.cpp:2:18: error: QLabel: No such file or directory
hello.cpp: In function ‘int main(int, char**)’:
hello.cpp:6: error: ‘QApplication’ was not declared in this scope
hello.cpp:6: error: expected `;' before ‘app’
hello.cpp:7: error: ‘QLabel’ was not declared in this scope
hello.cpp:7: error: ‘label’ was not declared in this scope
hello.cpp:7: error: expected type-specifier before ‘QLabel’
hello.cpp:7: error: expected `;' before ‘QLabel’
hello.cpp:9: error: ‘app’ was not declared in this scope
hello.cpp: At global scope:
hello.cpp:4: warning: unused parameter ‘argc’
hello.cpp:4: warning: unused parameter ‘argv’
make: *** [hello.o] Error 1
通過分析得知無法找到頭文件,從而下面的都出錯。
於是猜想系統默認的qmake是Qt3中的程序。
使用全路徑編譯如下:
$ /usr/local/Trolltech/Qt-4.6.2/bin/qmake -project
$ /usr/local/Trolltech/Qt-4.6.2/bin/qmake hello.pro
$make
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/Trolltech/Qt-4.6.2/mkspecs/linux-g++ -I. -I/usr/local/Trolltech/Qt-4.6.2/include/QtCore -I/usr/local/Trolltech/Qt-4.6.2/include/QtGui -I/usr/local/Trolltech/Qt-4.6.2/include -I. -I. -o hello.o hello.cpp
g++ -Wl,-O1 -Wl,-rpath,/usr/local/Trolltech/Qt-4.6.2/lib -o hello hello.o -L/usr/local/Trolltech/Qt-4.6.2/lib -lQtGui -L/usr/local/Trolltech/Qt-4.6.2/lib -L/usr/X11R6/lib -lQtCore -lpthread
$ls
hello hello.cpp hello.o hello.pro Makefile
出現了可執行程序hello,編譯成功,運行也成功,於是斷定,默認的qmake不是我們需要的/usr/local/Trolltech/Qt-4.6.2/bin/qmake。
下面我們來定位以下系統中有多少個qmake程序:
$locate qmake
/usr/bin/qmake
/usr/include/kdevelop/buildtools/parsers/qmake
/usr/lib/qt-3.3/bin/qmake
/usr/local/Trolltech/Qt-4.6.2/bin/qmake
只列出部分我們需要的,好了,可以看到有qt-3.3的qmake混跡其中,而/usr/bin在環境變量中,可能第一個/usr/bin/qmake就是我們運行出錯的qmake。我們來研究一下/usr/bin/qmake:
$which qmake
/usr/bin/qmake
可以看到這個就是默認的qmake
$ls -l /usr/bin/qmake
rwxrwxrwx 1 root root 23 Mar 23 12:59 /usr/bin/qmake -> ../lib/qt-3.3/bin/qmake
$ file /usr/bin/qmake
/usr/bin/qmake: symbolic link to `../lib/qt-3.3/bin/qmake'
明白了吧,這個是qt-3.3中qmake的一個軟鏈接。只要修改一下它指向的位置就可以了。
$su
Password:
#ln -s /usr/local/Trolltech/Qt-4.6.2/bin/qmake /usr/bin/qmake
ln: creating symbolic link `/usr/bin/qmake' to `/usr/local/Trolltech/Qt-4.6.2/bin/qmake': File exists
#ln -s /usr/local/Trolltech/Qt-4.6.2/bin/qmake /usr/bin/qmake1
#ls /usr/bin/qmak*
/usr/bin/qmake /usr/bin/qmake1
# mv /usr/bin/qmake1 /usr/bin/qmake
mv: overwrite `/usr/bin/qmake'?yes
# ls /usr/bin/qmak*
/usr/bin/qmake
#ls -al /usr/bin/qmake
lrwxrwxrwx 1 root root 39 Mar 25 16:18 /usr/bin/qmake -> /usr/local/Trolltech/Qt-4.6.2/bin/qmake
成功了,下面刪除剛才的Makefile,重新編譯:
$ qmake -project
QFileInfo::absolutePath: Constructed with empty filename
$ qmake hello.pro
$ make
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/Trolltech/Qt-4.6.2/mkspecs/linux-g++ -I. -I/usr/local/Trolltech/Qt-4.6.2/include/QtCore -I/usr/local/Trolltech/Qt-4.6.2/include/QtGui -I/usr/local/Trolltech/Qt-4.6.2/include -I. -I. -o hello.o hello.cpp
g++ -Wl,-O1 -Wl,-rpath,/usr/local/Trolltech/Qt-4.6.2/lib -o hello hello.o -L/usr/local/Trolltech/Qt-4.6.2/lib -lQtGui -L/usr/local/Trolltech/Qt-4.6.2/lib -L/usr/X11R6/lib -lQtCore -lpthread
$ ls
hello hello.cpp hello.o hello.pro Makefile
問題解決。