红联Linux门户
Linux帮助

Fedora安装Qt4后qmake自动连接至Qt3的解决办法

发布时间:2015-04-13 10:11:42来源:linux网站作者:Qucix

在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上安装完Qt4.6.2后,设置好环境变量,用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

问题解决。