从WIndows中的命令提示符运行QT应用程序

Running a QT application from Command promt in WIndows

本文关键字:运行 QT 应用程序 命令提示符 WIndows      更新时间:2023-10-16

我制作了一个小的QT应用程序,我正试图通过Windows上的命令提示符运行它:

#include <QMainWindow>
#include <QLabel>
int main(int argc,char* argv[])
{
QMainWindow a(argc,argv)
QLabel *NewLabel = new QLabel("Hi i am a label");
NewLabel->show();
return a.exec();
}

执行qmake -project之后然后CCD_ 2然后我尝试make,这里它失败了,并出现以下错误:

D:TestPrg>make
make -f Makefile.Debug
make[1]: Entering directory `D:/TestPrg'
Makefile.Debug:58: *** missing separator.  Stop.
make[1]: Leaving directory `D:/TestPrg'
make: *** [debug] Error 2

如果我们查看makefile.debug的第58行,并在"<<"之前添加一个TAB,它会在其他行号处抱怨。所以我觉得编译器选项有问题,有人能指导它如何工作吗。

感谢

我刚刚在我的机器上做了一个示例。代码如下,但您至少有几个错误,即:

  • 您使用QMainWindow作为应用程序,因为它似乎与QApplication相反。这不会编译。

  • 相应地,您需要包括QApplication而不是QMainWindow。

  • 在主函数的第一条语句之后,您缺少一个分号。

  • 您在堆上构造了一个QLabel,这是不必要的。在这个特定的场景中,它可以是一个简单的堆栈对象。

  • 您使用调用qmake作为qmake -foo,而不仅仅是qmakemake foo

  • 您正试图在Windows命令提示符中使用"make",而不是nmakejom。如果您使用Visual Studio和MSVC,请不要将其与mingw、cygwin和其他东西混合使用。只需使用nmake,否则,对后一个选项使用make。

main.cpp

#include <QApplication>
#include <QLabel>
int main(int argc, char **argv)
{
QApplication a(argc, argv);
QLabel NewLabel("Hi i am a label");
NewLabel.show();
return a.exec();
}

main.pro

TEMPLATE = app
TARGET = main
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
SOURCES += main.cpp

构建并运行

* qmake
* nmake
* main.exe