从头开始为使用Qt的应用程序制作简约的构建文件

Making a minimalistic build file for a Qt-using application from scratch

本文关键字:文件 构建 应用程序 Qt 从头开始      更新时间:2023-10-16

出于纯粹的教育目的,我想学习如何从头开始创建自己的QT Makefile。

我已经成功地使用了QMake,但我仍然想学习如何制作一个非常简单的Makefile来运行一个基本的QT应用程序。

这是我想要使用 Makefile 编译的代码:

#include <QApplication>
#include <QMainWindow>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow m;
m.show();
return a.exec();
}

我没有任何特殊要求,也不需要任何特殊的原生包装。

是的!那是一次巨大的学习经历。:)

这是我创建的最小生成文件。

# In the first two lines, I define the source (.cpp) file and the desired output file to be created (./hello)
SOURCE = hello.cpp
OUTPUT = hello
# This variable is used as a quick reference to the QT install path. This is my custom install directory on Mac OS X.
QT_INSTALL_PATH = /usr/local/qt/5.9.1/clang_64
# This is where you would provide the proper includes depending on your project's needs
INCLUDES = -I$(QT_INSTALL_PATH)/lib/QtWidgets.framework/Headers
#This is the RPATH. To be honest, I am not completely sure of what it does, but it is neccesary.
RPATH = -Wl,-rpath,$(QT_INSTALL_PATH)/lib
#Here is where you link your frameworks
FRAMEWORKS = -F$(QT_INSTALL_PATH)/lib -framework QtCore -framework QtWidgets
# The main action that happens when you type "make"
all:
# We run the g++ command and make sure we use c++11 with the second argument. Then after that, we simply plugin in our previous variables.
g++ -std=c++11 $(SOURCE) $(INCLUDES) $(FRAMEWORKS) $(RPATH) -o $(OUTPUT)

如果您想知道,这是我的hello.cpp文件。这是一个非常基本的程序,只导入QtWidgets

#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.resize(320, 240);
window.show();
window.setWindowTitle(QApplication::translate("toplevel", "Welcome to QT!"));
return app.exec();
}

这很有趣,我确实学到了很多关于QT程序如何编译的知识。但是,将来我绝对计划坚持使用 qmake,因为它是一个非常棒的工具。