繁体   English   中英

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

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

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

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

是! 那真是一种学习经验。 :)

这是我创建的最小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,因为它是一个非常好的工具。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM