在Qt应用程序中编译多个.cpp文件

compiling multiple .cpp files in a Qt application

本文关键字:cpp 文件 编译 Qt 应用程序      更新时间:2023-10-16

我刚刚开始学习Qt,编译并执行了这个spinet

#include <qapplication.h>
#include <qlabel.h>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Linux is wonderful", 0);
app.setMainWidget(label);
label->show();
return app.exec();
}

我使用以下步骤使其运行:Qmake-项目

qmake .pro 文件

但是这里这段代码会写在一个.cpp文件中,所以我想问一下项目有多大当有多个.cpp文件时处理。在有多个.cpp文件的情况下,遵循哪些步骤进行编译和执行。请解释一下。

对于多个.cpp文件,您的 .pro 文件中应该包含以下内容:

SOURCES = main.cpp otherfile.cpp yet_another_file.cpp

。很多人会把它分散在多行上,就像这样,以使其更容易阅读:

SOURCES = main.cpp 
          otherfile.cpp  
          yet_another_file.cpp
如果您

使用的是Qt,我建议您使用Qt Creator.Creator可以轻松管理用于创建make文件的.pro文件。它还包括一个非常有用的调试器,其中包括我们这些来自 MSVS 的人的智能感知形式。

在Creator中,创建一个新的Qt Desktop应用程序,然后从那里开始。另外,Add New...Add Existing...是你的朋友。