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

时间:2017-08-01 22:21:16

标签: c++ qt makefile

出于纯粹的教育目的,我想学习如何从头开始创建自己的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();
}

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

1 个答案:

答案 0 :(得分:0)

是的!这是一次学习经历。 :)

这是我创建的最小Makefile。

# 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,因为它是一个非常棒的工具。