Qt程序编译失败

Qt program fails to compile

本文关键字:失败 编译 程序 Qt      更新时间:2023-10-16

我正在编写这个简单的Qt应用程序,但它显示以下错误。谁能解释我为什么会出现这些错误?下面是代码片段:

#include <QTextStream>
int main()
{
   QTextStream out(stdout);
   out << "console applicationn";
}

编译步骤如下:

qmake -project 
qmake .pro file 
make 

按照上述步骤操作后,以下是我得到的输出:

g++ -c -pipe -g -Wall -W -O2 -D_REENTRANT  -DQT_NO_DEBUG -DQT_THREAD_SUPPORT -DQT_SHARED -DQT_TABLET_SUPPORT -I/usr/share/qt3/mkspecs/default -I. -I. -I/usr/include/qt3 -o text.o text.cpp
text.cpp:1:23: error: QTextStream: No such file or directory
text.cpp: In function ‘int main()’:
text.cpp:5: error: ‘QTextStream’ was not declared in this scope
text.cpp:5: error: expected ‘;’ before ‘out’
text.cpp:6: error: ‘out’ was not declared in this scope
make: *** [text.o] Error 1

平台: Linux.

使用 qmakemake 而不是手动调用编译器。

cd YourProject/
qmake -project
qmake
make
./YourProjectTarget

并确保您调用的qmake版本是 Qt4 版本,而不是 Qt3。您似乎安装了两个版本,并且您可能正在调用Qt3版本。

试试这个:

qmake -version

输出应该是这样的

QMake version 2.01a
Using Qt version 4.8.1 in /usr/lib/x86_64-linux-gnu

为了确保您调用的是正确的qmake(Qt4),通常您可以将qmake命令替换为 qmake-qtX ,其中X是 Qt 版本。

cd YourProject/
qmake-qt4 -project
qmake-qt4
make
./YourProjectTarget