How can I pass the output of a command as a compiler flag through a Qt project file?

好久不见. 提交于 2019-12-04 17:47:58

Problem solved thanks to this link: http://robertcarlsen.net/blog/2009/01/06/qmake-xcode-bug-258

Here's a sample qt project I used to test it: qt.pro:

######################################################################
# Automatically generated by qmake (2.01a) Thu Apr 2 16:23:05 2009
######################################################################

TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

# Input
SOURCES += qt.cpp

QMAKE_CXXFLAGS += -DAPP_DATE=\\\"`date +'\"%a_%b_%d,_%Y\"'`\\\"
QMAKE_CXXFLAGS += -DAPP_VERSION=\\\"`git describe`\\\"

qt.cpp:

#ifndef APP_DATE
#define APP_DATE "1/1/1970"
#endif

#ifndef APP_VERSION
#define APP_VERSION "local-dev"
#endif

#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QString version = QString("version ") + APP_VERSION + ' ' + APP_DATE;
    QLabel *label = new QLabel(version);
    label->show();
    return app.exec();
}

You can also use

QMAKE_CXXFLAGS += -DAPP_VERSION=\\\"$$system(git describe HEAD)\\\"

This will execute the git command only once during the qmake run which might speed up compilation for large projects. However, you must make sure to run qmake and make clean after pulling from the repository.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!