2

I'm trying to add the output of "git describe" to the about window of my application, so it's easier to find out what version of the application people use.

I can do it by adding the following compiler flag: -DAPP_VERSION="$(git describe HEAD)"

But since the project is based on qmake, I would like to find a way to put this into the Qt project file. Is this possible? And if so, how?

edit: I tried adding the following:

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

But it just gave me "-DAPP_VERSION=", so I suppose I have to use some escape characters, but I don't know which ones and where. :/

2 Answers 2

5

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();
}
1
  • doesn't this have the same issue described above? if you switch branches and qt.pro and qt.cpp didn't change, it won't be recompiled... but the other branch might have changes in other files (and have a different hash too)
    – mBardos
    Aug 12, 2016 at 12:10
1

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.

2
  • Not robust, as any changes (e.g., dirty) will be easily missed.
    – 0 _
    Jun 23, 2015 at 13:15
  • This functionality is neccessary for builds that are given to others. And in this case, you should have a build script which makes sure that the tree is not dirty and that a full rebuild is done (including a qmake run). Mar 21, 2016 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.