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

醉酒当歌 提交于 2019-12-09 20:22:30

问题


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. :/


回答1:


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();
}



回答2:


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.



来源:https://stackoverflow.com/questions/852437/how-can-i-pass-the-output-of-a-command-as-a-compiler-flag-through-a-qt-project-f

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